Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What logic determines the insert order of Entity Framework 6

Tags:

So, I have a DBContext, and I am doing the following operations:

dbContext.SomeTables1.Add(object1) dbContext.SomeTables2.AddRange(objectArray2) dbContext.SomeTables3.AddRange(objectArray3) dbContext.SaveChanges(); 

The EF doesn't insert the db records in this order, it inserts them in a random order. To insert them in the same order, I have to do a dbContext.SaveChanges() after each addition. This is not an efficient solution and in my case, it is taking 10 seconds to do all my inserts, while the random order with one save takes around 3 seconds.

N.B. I need the right order to solve a deadlock issue.

My questions are:

  • Is this issue resolved in EF7?
  • I can profile EF and determine the random order, however, is there a guarantee that it will be consistently with the same random order or does it change between requests? (I can adopt my other code if the answer to this question is positive).
  • Is there a better way of maintaining the order than dbContext.SaveChanges() on every addition?
like image 505
Adam Avatar asked Jan 14 '16 17:01

Adam


People also ask

Which function is used for insert operation in Entity Framework?

Insert Data Use the DbSet. Add method to add a new entity to a context (instance of DbContext ), which will insert a new record in the database when you call the SaveChanges() method.

What is include in Entityframework?

Entity Framework Classic Include The Include method lets you add related entities to the query result. In EF Classic, the Include method no longer returns an IQueryable but instead an IncludeDbQuery that allows you to chain multiple related objects to the query result by using the AlsoInclude and ThenInclude methods.

What are the different relationship patterns in Entity Framework?

Entity framework supports three types of relationships, same as database: 1) One-to-One 2) One-to-Many, and 3) Many-to-Many. We have created an Entity Data Model for the SchoolDB database in the Create Entity Data Model chapter.

In what number of approaches can an entity item be created?

There are three approaches to model your entities in Entity Framework: Code First, Model First, and Database First.


1 Answers

  • There is no way you can specify a save order in EF6 or EF Core (initially named EF7).
  • The issue is not resolved in EF Core (initially named EF7) since this is not an issue.
  • The order will be the same if the predecessor is the same (which will likely rarely happen)

When you call SaveChanges, all entities are ordered from an internal order in the method “ProduceDynamicCommands” then sorted again by the method “TryTopologicalSort” which loops to add command with no predecessor left (if you add A and B and A depend on B, then B will be inserted before A)

You are left to insert by batch addition.

Since it takes you 3 seconds to perform your insert, I will assume you have thousands of entities and performing bulk insert may improve your performance to reduce the 10 seconds to less, and then maybe the initial 3 seconds!

Here are 2 libraries I can recommend:

  • https://efbulkinsert.codeplex.com/
    • FREE but doesn’t work with all kind of associations and inheritances
  • http://entityframework-extensions.net/
    • PAID but support everything

Disclaimer: I'm the owner of the Entity Framework Extensions project.

like image 171
Jonathan Magnan Avatar answered Oct 02 '22 15:10

Jonathan Magnan