How to use DBContext.Add/Attach (using EF CodeFirst 4.1) with nested opbjects

懵懂的女人 提交于 2019-12-23 12:18:24

问题


Problem: When adding an object "Order" to my dbcontext, all nested objects of the order gets "readded" to the database, though the nested objects is static data and only a reference shoudl be added in the database.

Example: The database holds 0 orders, and 3 items.

I add one order with 2 items.

Now the database hold 1 order, and 5 items. The two items in the order has been "readded" to the database, even though the items had the right primary keys before db.SaveChanges().

I realize that i may be able to attach the existing items to the dbcontext before saving changes, but is that really the only way to go? Can't EF figure out that to item already exists when the primary key matches an existing item?

Does anyone know if this is different in the new version of EF CodeFirst?


回答1:


No EF cannot figure if entities are existing one or new one - both Add and Attach commands are graph oriented operations. You call them on one entity in the graph and they traverse all relations (and their relations and so on) and perform the operation for them as well.

You must figure correct state of each entity in the graph for example by using:

dbContext.Orders.Add(newOrder);
foreach(var item in newOrder.Items) {
    dbContext.Entry(item).State = EntityState.Unchanged;
}
dbContext.SaveChanges();

You can use the reverse operation by calling Attach(newOrder) and set the order to Added state. The main difference will come with independent associations (for example many-to-many relations). The first approach will correctly add new relation between order and each item whereas second will not unless you manually set each relation to Added state (and changing state for relations is more complex).



来源:https://stackoverflow.com/questions/12353592/how-to-use-dbcontext-add-attach-using-ef-codefirst-4-1-with-nested-opbjects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!