entity-framework

why does entity framework saves only the last item of a list?

泪湿孤枕 提交于 2021-01-28 08:40:35
问题 I have 2 objects with one to many relation. I'm trying to add multiple ProductImages with the same ProductId to the data base, while using a generic repository. I have tried saving a list of ProductImage to a repository with dbSet.AddRange . after that i saved it with context.SaveChanges(); i have also tried looping trough the list of ProductImage and adding each item with dbSet.Add .and than context.SaveChanges() . repository methods: public virtual void Add(TEntity entity) { dbSet.Add

EF Core context not disposing of change tracking entities

和自甴很熟 提交于 2021-01-28 08:18:05
问题 Using EF Core 2.1 to insert 135k records. Once the request is finished, I would expect the context to be disposed of and along with it, the ChangeTracking entities to be cleared, however, it's holding on to all 135k records and using a lot of memory in the process. We are using ASP.NET Core 2.1 and injecting the EF Context from the DI container. Since the DI container should dispose of the scoped context at the end of the request, it should not be holding on to these values. Even calling

Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar while executing a stored procedure with Entity Framework

独自空忆成欢 提交于 2021-01-28 07:33:24
问题 I am getting error Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar' while executing a stored procedure with Entity Framework. This is my stored procedure: CREATE PROCEDURE [dbo].[usp_RolesList] (@WhereCond VARCHAR(50)) AS BEGIN SET NoCount ON DECLARE @SQLQuery AS VARCHAR(MAX) SET @SQLQuery = 'Select * From dbo.AspNetRoles ' + @WhereCond EXECUTE sp_Executesql @SQLQuery END and this is my c# code that I am using for executing the results var idParam = new SqlParameter {

How to Update a Many To Many relationship

こ雲淡風輕ζ 提交于 2021-01-28 06:50:59
问题 I'm have this Many To Many Relationship: public class Role { [Key] public int role_Id { get; set; } public string Name { get; set; } public ICollection<LoginModel> Users { get; set; } public ICollection<Permission> Permissions { get; set; } public Role() { Users = new List<LoginModel>(); Permissions = new Collection<Permission>(); } } public class Permission { [Key] public int permi_Id { get; set; } public string Name { get; set; } public virtual ICollection<Role> Roles { get; set; } public

Update-Database - Error with (localdb)\MSSQLLocalDB

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 06:11:22
问题 I am getting the following issue when running Update-Database after running Add-Migration: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Specified LocalDB instance name is invalid. ) Sql local DB: In

Show Progressbar when loading data using EntityFrameWork in WPF

醉酒当歌 提交于 2021-01-28 06:08:09
问题 I want to show the progress of Data being fetched from Database in a Progressbar . Currently i am updating the content of a Button . Have no idea how to implement in this code . private async void FetchInvoicesDataFunc(object sender, RoutedEventArgs e) { ProgressBtn.Content = "Loading Data ..."; InvoiceGrid.ItemsSource = await Task.Run(FetchInvoiceDataAsync); ProgressBtn.Content = "Loaded !"; } private async Task<List<Invoice>> FetchInvoiceDataAsync() { List<Invoice> result; using(var context

Why is creating a new DbContext slower than a Dependency Injected one?

眉间皱痕 提交于 2021-01-28 05:38:30
问题 I recently determined that there are no significant performance gains from using a Dependency Injected DbContext in .NET Core and using async await calls as opposed to creating a new DbContext every time I want to access the DB. But now I need to know why. I did a much more granular test with System.Diagnostics.Stopwatch in my .NET Core 1.1 API services (which the controller is calling) in which I ran the stopwatch only when accessing the DB. The results were surprising. When using the

Where Not in List<string>

雨燕双飞 提交于 2021-01-28 05:20:16
问题 I have a List<string> that contains a number of string values. Can I filter a collection when doing a query with a list instead of a single value? Something like this: List<string> slist = new List<string>() { "val 1", "val 2", "val 3" }; var q = (from s in ctx.Shipments where !s.ShipName.ToList().Contains(slist)).ToList() 回答1: What you need is !slist.Contains(s.ShipName) like: (from s in ctx.Shipments where !slist.Contains(s.ShipName) ).ToList(); You may see this article for creating IN

ShadowProperties being reached by foreach before being created

只谈情不闲聊 提交于 2021-01-28 05:04:44
问题 I'm new to Entity Framework and I'm learning atm with the Pluralsight courses from Julie Lerman. I'm watching the 2nd course "Entity Framework Core 2: Mappings" but I'm using EF Core 2.1. Edit: So I decided comment everything out and follow the course again and it is working now BUT the generated migration in generating 2 columns that shouldn't be there: protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn<DateTime>( name: "BetterName_Created", table:

EntityFrameworkCore - How to inject IPrincipal when DbContextPool is enabled

只愿长相守 提交于 2021-01-28 04:41:32
问题 I have an aspnet core MVC application, and I need to inject the IPrincipal of the current request so that I know who is the current logged in user. I followed this article with no problem. Now I need the IPrincipal in my Dbcontext to auto-populate audit fields (CreatedBy, UpdatedBy, etc). Being naive I could inject the IPrincipal in my DbContext constructor: public MyDBContext(DbContextOptions<MyDBContext> options, IPrincipal principal) But I am using the DBContextPool , which reuses