问题
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