Entity framework 具有某个不可为null的int字段的Bulkinsert将导致InvalidOperationException,并显示DBNull。该字段不允许使用Value
我正在使用EntityFramework.BulkInsert from 试图将Entity framework 具有某个不可为null的int字段的Bulkinsert将导致InvalidOperationException,并显示DBNull。该字段不允许使用Value,entity-framework,entity-framework-6,bulkinsert,ef-bulkinsert,Entity Framework,Entity Framework 6,Bulkinsert,Ef Bulkinsert,我正在使用EntityFramework.BulkInsert from 试图将列表中的大量记录插入数据库 我得到的是一个关于int字段的异常,其中不允许值等于DBNull.Value 但是,它在模型中不是可为空的字段,因此其中至少有一个值0。将该值设置为null的概念不适用。事实上,我是在明确地设置值。我尝试了0以外的其他值,得到了相同的异常 为什么我会得到例外 细节 我有一个包含如下实体的列表: var foos = new List<Foo>(); using (var t
列表中的大量记录插入数据库
我得到的是一个关于int字段的异常,其中不允许值等于DBNull.Value
但是,它在模型中不是可为空的字段,因此其中至少有一个值0。将该值设置为null的概念不适用。事实上,我是在明确地设置值。我尝试了0以外的其他值,得到了相同的异常
为什么我会得到例外
细节
我有一个包含如下实体的列表:
var foos = new List<Foo>();
using (var transactionScope = new TransactionScope())
{
var foos = new List<Foo>();
foreach (var id in ids)
{
var foo = new Foo
{
SendAttempts = 0,
SendDate = DateTime.Now,
BarID = id
};
foos.Add(foo);
}
_entities.BulkInsert(foos);
_entities.SaveChanges();
transactionScope.Complete();
}
var foos=newlist();
其中,Foo是一个实体类,定义为:
public partial class Foo
{
public int id { get; set; }
public int SendAttempts { get; set; }
public Nullable<System.DateTime> LastSendAttempt { get; set; }
public string LastSendAttemptMessage { get; set; }
public Nullable<System.DateTime> SendDate { get; set; }
public int BarID { get; set; }
public virtual Bar Bar { get; set; }
}
公共部分类Foo
{
公共int id{get;set;}
公共int发送尝试{get;set;}
公共可为Null的LastSendAttrint{get;set;}
公共字符串LastSendAttempMessage{get;set;}
公共可为空的SendDate{get;set;}
公共int BarID{get;set;}
公共虚拟条{get;set;}
}
列表中填充了一个循环,该循环添加了如下实体:
var foos = new List<Foo>();
using (var transactionScope = new TransactionScope())
{
var foos = new List<Foo>();
foreach (var id in ids)
{
var foo = new Foo
{
SendAttempts = 0,
SendDate = DateTime.Now,
BarID = id
};
foos.Add(foo);
}
_entities.BulkInsert(foos);
_entities.SaveChanges();
transactionScope.Complete();
}
使用(var transactionScope=new transactionScope())
{
var foos=新列表();
foreach(id中的变量id)
{
var foo=新foo
{
发送尝试次数=0,
SendDate=DateTime。现在,
BarID=id
};
添加(foo);
}
_实体。批量插入(foos);
_entities.SaveChanges();
transactionScope.Complete();
}
异常发生在消息的BulkInsert行
列“SendAttempts”不允许DBNull.Value
我也有同样的问题。检查EF模型和数据库中的列顺序。必须是一样的