I’m saving a list of records (55,000) at single time using Entity Framework Core with this code in .NET 8:
await _context.Associatedjobs.AddRangeAsync(Allassociatedrecords);
await _context.SaveChangesAsync();
I get an error:
The instance of entity type ‘Students’ cannot be tracked because another instance with the same key value for {‘id’} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using ‘DbContextOptionsBuilder.EnableSensitiveDataLogging’ to see the conflicting key values.
I’m using EF Core with a SQLite database and the table does not have any primary key (which is not needed), but still I get this error while saving this large records.
Used this code in the program.cs
file of ASP.NET Core 8 Web API:
builder.Services.AddDbContext<AppDbContext>(options =>
{
options.UseSqlite(Sqlconnection);
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});
4