在库EFCore 3.6.1中使用EFCore.BulkExtensions 3.1 ( EFCore 3.1的最新版本)。
数据库服务器是server 2019。
下面是再现错误的代码。
具有来自另一个类的导航属性的简单Customer类:
public class Customer
{
public int ID { get; set; }
public String Name { get; set; }
public Cont Continent { get; set; }
}
public class Cont
{
public int ID { get; set; }
public String Name { get; set; }
}当我试图使用来自BulkInsert库的"BulkInsert“方法将实体插入到具有填充导航属性的客户中时,导航道具的值不会保存到数据库中:
Customer cust1 = new Customer
{
Continent = contList.Find(x => x.Name == "Europe"),
Name = "Wesson Co, Ltd."
};
Customer cust2 = new Customer
{
Continent = contList.Find(x => x.Name == "Asia"),
Name = "Tiranha, Inc."
};
// (checking the "Continent" props here shows them to be properly populated)
List<Customer> CustomerList = new List<Customer> { cust1, cust2 };
dbContext.BulkInsert(CustomerList);结果是数据库中的"ContinentID“列为NULL。
与往常一样,使用EF () works -将最后两行更改为:
dbContext.Customers.AddRange(cust1, cust2);
dbContext.SaveChanges();这很好用。但是我必须插入100万条记录,而SaveChanges()在这种情况下的性能很差。
我做错什么了吗?
使用另一个(较低)版本的BulkExtension没有帮助。更高的版本将无法工作,因为它们都以EFCore 5为目标,NetStandard 2.1是我的项目目前不支持的。
在EFCore.BulkExtension文档中找不到导航道具相关信息的任何提示或提及。
要查找所发送的SQL,只向我显示这样的查询
INSERT INTO dbo.Customers (ContinentID, Name) VALUES @p1, @p2因此,应该由BulkExtensions.BulkInsert()来正确地放置值,而它似乎没有这样做。
关键是,类似的代码已经工作了6个月,现在对于任何版本的BulkExtension库,都使用了一个简单的场景(如上面所述)。所以,我的代码或方法一定有问题,但找不到。
更新
将包EFCore.BulkExtensions降级到3.1.6会给我带来不同的错误。仍然不起作用,但以下是错误:
System.InvalidOperationException : The given value 'Customer' of type String from the data source cannot be converted to type int for Column 2 [ContinentID] Row 1.
----> System.FormatException : Failed to convert parameter value from a String to a Int32.
----> System.FormatException : Input string was not in a correct format.发布于 2021-07-10 21:02:09
现在看来,这是EFCore.BulkExtensions库中的一个bug -- 3.2.1到3.3.5版本将正确处理它(大部分情况下),而3.3.6-3.6.1版本则不正确。
在撰写本文时,使用3.3.5版本获得最稳定的结果。
( EFCore 5的5.x版没有数据)
https://stackoverflow.com/questions/68306638
复制相似问题