简体   繁体   中英

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

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?

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?

这是您注释自动递增字段的方式

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

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

On your Id field, Like:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM