Entity Framework BulkInsert not inserting child entities

不羁的心 提交于 2020-01-01 19:53:11

问题


I have 2 tables INS_Staging_UMLERTransaction(Parent) and INS_Staging_UMLERBlueCard(Child).

I need to insert 1000 records, when I use bulk insert it is inserting only parent table. The following is my code.

 _indnCon.BulkInsert(_DataToTrans);                                       
 _indnCon.BulkInsert(_DataToTrans.SelectMany(m => 
                      m.INS_Staging_UMLERBlueCard));
 _indnCon.BulkSaveChanges();

回答1:


There is no BulkInsert or BulkSaveChanges method in Entity Framework Extended.

So in my answer, I will assume you are using Entity Framework Extensions library.


Since the v3.12.6 (Release Note), a IncludeGraph option has been added to include child entities.

Example

_indnCon.BulkInsert(_DataToTrans, operation => operation.IncludeGraph = true);  

Note: You don't have to call BulkSaveChanges after calling BulkInsert. Bulk Operation is performed directly in the database.

Answer Sub-Question:

I'm getting an exception when I use IncludeGraph. The exception is "A default DbContext context must exist, or a context factory must be provided (EntityFrameworkManager.ContextFactory). This setting is required for some features like IncludeGraph."

This error mean you don’t have a default constructor for your context. A way to build a new context is required for this feature.

Here is an example how to specify a context factory when no default constructors exists:

EntityFrameworkManager.ContextFactory = context => new CurrentContext(My.ConnectionString);


来源:https://stackoverflow.com/questions/44898323/entity-framework-bulkinsert-not-inserting-child-entities

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