Support SaveChanges while iterating a query
See original GitHub issueusing (var db = new UnicornStoreContext())
{
foreach (var p in db.Products)
{
db.Products.Add(new Product { DisplayName = "Copy of " + p.DisplayName });
db.SaveChanges();
}
}
SqlException: New transaction is not allowed because there are other threads running in the session. System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
Obviously this is a bit of an anti-pattern since the SaveChanges should go outside the loop, but this is just a boiled down repro from a case where it does make sense (calling another component like Idenitty that saves to the database as part of the API call).
foreach (var role in db.PreApprovals
.Where(p => p.UserEmail == user.Email)
.Select(p => p.Role))
{
await UserManager.AddToRoleAsync(user, role);
}
Model from the repro…
public class UnicornStoreContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptions options)
{
options.UseSqlServer("Server=(localdb)\\v11.0;Database=UnicornStore;Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
public class Product
{
public int ProductId { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
public decimal MSRP { get; set; }
public decimal CurrentPrice { get; set; }
}
Issue Analytics
- State:
- Created 9 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Entity Framework - Loop then call SaveChanges
Note : SaveChanges() operates within a transaction. SaveChanges() will roll back that transaction and throw an exception if any of the dirty ...
Read more >Entity Framework doesn't like SaveChanges in a for loop.. ...
So I'm working on a program which is a service that loops through records and calculates averages and then saves the changes to...
Read more >Async query and save - EF6
SaveChanges begins to push the new Blog to the database; SaveChanges completes; Query for all Blogs is sent to the database; Query returns...
Read more >ExecuteUpdate and ExecuteDelete - EF Core
ExecuteUpdate and ExecuteDelete are a way to save data to the database without using EF's traditional change tracking and SaveChanges() ...
Read more >Gotcha: Entity Framework gets slow in long Iteration Loops
I've been running into a common issue with Entity Framework where using a dbContext through many context operations can drastically slow ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dazbradbury I read through to understand again. I don’t think it’s something we are planning to revisit.
Understood. We’re happy to rely on the analyser, I guess if anyone else runs into this they can comment / follow this issue. Thanks for letting us know in any case so we can plan around it.