Mohamed Said
Mohamed Said

Reputation: 553

Bulk Insert with child entities using EF6 extensions

I'm getting an error everytime I try to bulk insert using EF6 Extensions library

When using IncludeGraph, some options must be set in the IncludeGraphBuilder (See: https://entityframework-extensions.net/include-graph). The following options must be specified in IncludeGraphBuilder: ColumnPrimaryKeyExpression, LambdaPrimaryKeyExpression

The schema is as follows
Customer(PK,____,AddressId)
CustomerAddress(PK,____,AddressLookupId)
CustomerPhone(PK,___,CustomerId)
AddressCities(PK,CityName)
AddressLookup(PK,Zip,CityId, StateId)

I have the following as a lookup table
AddressStates(PK,_____)

options for filling the data as follows

options.InsertIfNotExists = true;
options.IncludeGraph = true;
options.IncludeGraphOperationBuilder = operation =>
                {
                    switch (operation)
                    {
                      case BulkOperation<Customer> customer:
                            customer.InsertIfNotExists = true;
                            customer.ColumnPrimaryKeyExpression = x => new { 
                            x.FirstName, x.MiddleName, x.LastName};
                            customer.AutoMapOutputDirection = true;
                            //customer.LambdaPrimaryKeyExpression = 
                            customer.AutoMapIdentityExpression;
                            break;
                        case BulkOperation<CustomerAddress> customerAddress:
                            customerAddress.InsertIfNotExists = true;
                            customerAddress.ColumnPrimaryKeyExpression = x => 
                            new { x.Address };
                            customerAddress.AutoMapOutputDirection = true;
                            //customer.LambdaPrimaryKeyExpression = 
                            customer.AutoMapIdentityExpression;
                            break;
                        case BulkOperation<CustomerPhone> customerPhone:
                            customerPhone.InsertIfNotExists = true;
                            customerPhone.ColumnPrimaryKeyExpression = x => 
                            x.PhoneNumber;
                            //customerPhone.LambdaPrimaryKeyExpression = 
                            customerPhone.AutoMapIdentityExpression;
                            customerPhone.AutoMapOutputDirection = true;
                            break;
                        case BulkOperation<AddressCity> addressCity:
                            addressCity.InsertIfNotExists = true;
                            addressCity.ColumnPrimaryKeyExpression = x => 
                            x.City;
                            //addressCity.LambdaPrimaryKeyExpression = 
                            addressCity.AutoMapIdentityExpression;
                            addressCity.AutoMapOutputDirection = true;
                            break;
                        case BulkOperation<AddressLookup> addressLookup:
                            addressLookup.InsertIfNotExists = true;
                            addressLookup.ColumnPrimaryKeyExpression = x => 
                          new { x.Zip, x.CityId, x.StateId };
                            //addressLookup.LambdaPrimaryKeyExpression = 
                            addressLookup.AutoMapIdentityExpression;
                            addressLookup.AutoMapOutputDirection = true;
                            break;

I also tried setting the ColumnPrimaryKeyExpression to map the columns that should stay unique and no luck.

Update 1 : Added the Customer options inside the graph builder, error has changed to violation of FK Constraint on City, even though it's set to only allow cities that doesn't exist.

Upvotes: 1

Views: 1238

Answers (2)

Mohamed Said
Mohamed Said

Reputation: 553

I was able to solve the problem by adding
<EntityType>.AllowDuplicateKeys = false;
and I had a null key value in some records so I had to filter them out

Upvotes: 1

Tim
Tim

Reputation: 2912

You aren't setting any settings for Customer. That likely is the issue

Upvotes: 1

Related Questions