1

每次尝试使用EF6 扩展库进行批量插入时都会出错

使用时IncludeGraph,必须在IncludeGraphBuilder(参见: https ://entityframework-extensions.net/include-graph )中设置一些选项。必须在以下选项中指定IncludeGraphBuilder: ColumnPrimaryKeyExpression, LambdaPrimaryKeyExpression

架构如下
Customer(PK,____,AddressId)
CustomerAddress(PK,____,AddressLookupId)
CustomerPhone(PK,___,CustomerId)
AddressCities(PK,CityName)
AddressLookup(PK,Zip,CityId, StateId)

我有以下查找表
AddressStates(PK,____)

填充数据的选项如下

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;

我还尝试设置 ColumnPrimaryKeyExpression 来映射应该保持唯一且没有运气的列。

更新 1:在图形构建器中添加了客户选项,错误已更改为违反城市的 FK 约束,即使它设置为仅允许不存在的城市。

4

2 回答 2

1

您没有为客户设置任何设置。这可能是问题

于 2020-05-28T16:41:55.613 回答
1

我能够通过添加来解决问题,
<EntityType>.AllowDuplicateKeys = false;
并且我在某些记录中有一个空键值,所以我不得不将它们过滤掉

于 2020-05-28T18:42:40.060 回答