In Entity Framework 7 what would be the best way to do batchInsert to a table which is mapped using entity framework but I want to make sure that this is one transaction so either all objects are inserted or none?
THanks for hints!
In Entity Framework 7 what would be the best way to do batchInsert to a table which is mapped using entity framework but I want to make sure that this is one transaction so either all objects are inserted or none?
THanks for hints!
3
Answers
Try using a combination of Transactions & SaveChanges() method, I can share a sample code with you:
Little bit changes on @Zohair post.
You can replace foreach loop by following statement.
dbContext.YourEntities.AddRangeAsync(entities); //Entities will be your List or Collection.
There is no need having a transaction.
This way if something fail with any of your objects during save, everything rollsback.