Cannot insert the value NULL into column 'Id', Entity framework

我与影子孤独终老i 提交于 2020-12-06 07:49:52

问题


I have the following model:

Services.StradaDataReview2Model.UOSChangeLog:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

public int Accident_nr { get; set; }

public int Refnr { get; set; }

public int Action { get; set; }

public string Old_data { get; set; }

public string New_data { get; set; }

public DateTime SearchedFromDate { get; set; }

public DateTime SearchedToDate { get; set; }

public DateTime Changed { get; set; }

public string Username { get; set; }

public string Comment { get; set; }

Contracts.DataContracts.UOSChangeLog

public class UOSChangeLog
{
    public int Id { get; set; }
    public int Accident_nr { get; set; }
    public int Refnr { get; set; }
    public int Action { get; set; }
    public string Old_data { get; set; }
    public string New_data { get; set; }
    public DateTime SearchedFromDate { get; set; }
    public DateTime SearchedToDate { get; set; }
    public DateTime Changed { get; set; }
    public string Username { get; set; }
    public string Comment { get; set; }
}

Here Is how I Insert:

internal static bool SaveUOSChangeLog(List<Contracts.DataContracts.UOSChangeLog> values, string user)
{
    try
    {
        using (var ctx = new StradaDataReviewContext2())
        {
            var newVal = Mapper.Map<List<Contracts.DataContracts.UOSChangeLog>, List<Services.StradaDataReview2Model.UOSChangeLog>>(values);
            foreach(var val in newVal)
            {
                val.Username = user;
                val.Changed = DateTime.Now;
                ctx.UOSChangeLog.Add(val);
            }

            ctx.SaveChanges();
            return true;
        }
    }
}

When I run the code, I get the following error:

InnerException = {"Cannot insert the value NULL into column 'Id', table 'StradaDataReview.dbo.UOSChangeLog'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

How can I make an auto increment of the Id column?


回答1:


I guess you are using Code First, which by convention will set the Id property as the primary key in the corresponding table that by default will have been defined as 'Identity'. I think that it is not necessary to set the [DatabaseGenerated(DatabaseGeneratedOption.Identity)] attribute. I would check first the database to see if in the corresponding table the Id column has been created as an Identity column. Also are you defining anything using Fluent API?




回答2:


This is how you annotate an auto incremented field

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]



回答3:


Try to Add: [DatabaseGenerated(DatabaseGeneratedOption.None)] Instead of Identity

On your Id field, Like:

[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }



回答4:


Your ID is primary key - so nulls are not allowed. It is because your record's ID must be unique.



来源:https://stackoverflow.com/questions/36080872/cannot-insert-the-value-null-into-column-id-entity-framework

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