获取“无法将 NULL 值插入列”当尝试使用 DbContext 使用 .Add() 方法保存时。请检查我的 POCO 和保存方法
首先使用代码,所有内容似乎都与下面的内容不同,当我使用 ObjectContext 并调用 context.PCBuilds.AddObject(pcBuild) 时,它也可以工作,但在切换到 DbContext 后,它给了我错误。
EFDbContext context = new EFDbContext();
public ActionResult Index()
{
PCBuild pcBuild = new PCBuild();
pcBuild.BuildID = 34245;
pcBuild.BuildName = "Test99";
pcBuild.MarkUp = 25;
pcBuild.BDetails = new List<BDetail>();
context.PCBuilds.Add(pcBuild);
//repository.PCBuilds.Attach(pcBuild);
context.SaveChanges();
return View();
}
给我:无法将值 NULL 插入表“C:\USERS\ADMIN\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\NEOCART\NEOCART.WEBUI\APP_DATA\NEODBX.MDF.dbo.PCBuilds”列“BuildID”中';列不允许为空。 INSERT 失败。
其中 BuildID 在调用 SaveChanges 之前已明确设置。似乎由于某种原因调用 .Add(pcBuild) 不会添加填充的对象,并且当调用 savechanges 时它会尝试插入空的 PCBuild ?
这是 POCO 的
public class PCBuild
{
[Key]
public int BuildID { get; set; }
public string BuildName { get; set; }
public string Socket { get; set; }
public decimal? MarkUp {get; set;}
[InverseProperty("PCBuild")]
public virtual ICollection<BDetail> BDetails { get; set; }
}
public class BDetail
{
[Key]
public int LineID { get; set; }
[ForeignKey("PCBuild")]
public int BuildID { get; set; }
[ForeignKey("Product")]
public int ProductID { get; set; }
public bool? IsSelected { get; set; }
[InverseProperty("BDetails")]
public virtual PCBuild PCBuild { get; set; }
[InverseProperty("BDetails")]
public virtual Product Product { get; set; }
}
Used code first and everything appears to work apart from the below which also worked before when I used ObjectContext and called context.PCBuilds.AddObject(pcBuild) but after switching to DbContext it's giving me the error.
EFDbContext context = new EFDbContext();
public ActionResult Index()
{
PCBuild pcBuild = new PCBuild();
pcBuild.BuildID = 34245;
pcBuild.BuildName = "Test99";
pcBuild.MarkUp = 25;
pcBuild.BDetails = new List<BDetail>();
context.PCBuilds.Add(pcBuild);
//repository.PCBuilds.Attach(pcBuild);
context.SaveChanges();
return View();
}
Giving me the: Cannot insert the value NULL into column 'BuildID', table 'C:\USERS\ADMIN\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\NEOCART\NEOCART.WEBUI\APP_DATA\NEODBX.MDF.dbo.PCBuilds'; column does not allow nulls. INSERT fails.
Where as BuildID was clearly set before the SaveChanges is called. Appears that calling the .Add(pcBuild) doesn't add the populated object for some reason and when savechanges is called it attempts to insert an empty PCBuild ?
Here are the POCO's
public class PCBuild
{
[Key]
public int BuildID { get; set; }
public string BuildName { get; set; }
public string Socket { get; set; }
public decimal? MarkUp {get; set;}
[InverseProperty("PCBuild")]
public virtual ICollection<BDetail> BDetails { get; set; }
}
public class BDetail
{
[Key]
public int LineID { get; set; }
[ForeignKey("PCBuild")]
public int BuildID { get; set; }
[ForeignKey("Product")]
public int ProductID { get; set; }
public bool? IsSelected { get; set; }
[InverseProperty("BDetails")]
public virtual PCBuild PCBuild { get; set; }
[InverseProperty("BDetails")]
public virtual Product Product { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 PCBuild.BuildID 属性上的“noreferrer">StoreGeneeratedAttribute。它不仅是一个键,而且是
IDENTITY
字段。更新
实际上,应该是
[DatabaseGenerate(DatabaseGenerationOption.Identity)]< /code> 注释。上面链接的文章描述了早期的 CTP 版本。
更新2
等等,密钥是由应用程序生成的,它不是数据库中的身份列?将注释更改为
[DatabaseGenerate(DatabaseGenerationOption.None)]
,重新创建上下文并重建应用程序。Use StoreGeneratedAttribute on the
PCBuild.BuildID
property. It is not only a key butIDENTITY
field.UPDATE
Actually, it should be
[DatabaseGenerated(DatabaseGenerationOption.Identity)]
annotation. The article linked above describes early CTP version.UPDATE 2
Wait, the key is being generated by the app, it is not an identity column in database? Change annotation to
[DatabaseGenerated(DatabaseGenerationOption.None)]
, re-create the context and rebuild the application.我不太熟悉 Code First 方法,但是当您将 BuildID 指定为 [Key] 字段时,是否会将其设置为数据库中的自动标识字段?
因此,它可能会阻止您尝试写入它。
尝试删除 [Key] 标识符,然后重新创建数据库。那么你可以保存对象吗?
I'm not really familiar with the Code First approach, but could it be that when you specify the BuildID as being a [Key] field, it is setting it as an auto identity field in the database?
As such it may be blocking your attempt to write to it.
Try removing the [Key] identifier, then recreate the database. Can you then save the object ok?