Getting “Cannot insert the value NULL into column” when trying to save with .Add() method using DbContext . Please check my POCO's and save method

蹲街弑〆低调 提交于 2019-12-23 19:12:06

问题


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; }


}

回答1:


Use StoreGeneratedAttribute on the PCBuild.BuildID property. It is not only a key but IDENTITY 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.




回答2:


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?



来源:https://stackoverflow.com/questions/7739222/getting-cannot-insert-the-value-null-into-column-when-trying-to-save-with-add

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!