1

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.

Mohamed Said
  • 524
  • 7
  • 27

2 Answers2

1

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

Tim
  • 2,878
  • 1
  • 14
  • 19
1

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

Mohamed Said
  • 524
  • 7
  • 27