无法将NULL值插入实体框架的"Id"列中 [英] Cannot insert the value NULL into column 'Id', Entity framework

查看:63
本文介绍了无法将NULL值插入实体框架的"Id"列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

Services.StradaDataReview2Model.UOSChangeLog:

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

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

这是我的插入方式:

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 = {无法将值NULL插入列'Id',表'StradaDataReview.dbo.UOSChangeLog';列不允许空值.INSERT失败.\ r \ n该语句已终止.}

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."}

如何自动增加 Id 列?

推荐答案

我猜您使用的是Code First,按照惯例,它会将ID属性设置为对应表中的主键,默认情况下,该表将被定义为'身份'.我认为没有必要设置[DatabaseGenerated(DatabaseGeneratedOption.Identity)]属性.我将首先检查数据库,以查看在相应表中是否已将Id列创建为Identity列.您还在使用Fluent API定义任何内容吗?

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?

这篇关于无法将NULL值插入实体框架的"Id"列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆