将项目添加到 Entity Framework 上下文时出现 DbUpdateException

DbUpdateException when adding item to Entity Framework Context

我有以下两个类:

public class Record
{
    public int RecordId { get; set; }

    public DateTime? InsertDate { get; set; } = DateTime.Now;
    public DateTime BookingDate { get; set; }
    public string AmountTypeName { get; set; }
    public double? Amount { get; set; }
    public string BookingAccountID { get; set; }
    public string AccountCurrency { get; set; }
    public string ClientCurrency { get; set; }
    public string AffectsBalance { get; set; }
    public double? AmountAccountCurrency { get; set; }
    public string AmountClientCurrency { get; set; }

    public int UnifiedInstrumentCode { get; set; }
    public InstrumentInfo InstrumentInfo { get; set; }
}

public class InstrumentInfo
{
    [Key]
    public int UnifiedInstrumentCode { get; set; }
    public ICollection<Record> Record { get; set; }

    public string AssetType { get; set; }
    public int UnderlyingInstrumentUic { get; set; }
    public string UnderlyingInstrumentSubType { get; set; }
    public string InstrumentSymbol { get; set; }
    public string InstrumentDescription { get; set; }
    public string InstrumentSubType { get; set; }
    public string UnderlyingInstrumentAssetType { get; set; }
    public string UnderlyingInstrumentDescription { get; set; }
    public string UnderlyingInstrumentSymbol { get; set; }
}

我想用作 EF6 的上下文。

我按以下方式定义了上下文:

public class TransactionsContext: DbContext
{
    public DbSet<Record> Records { get; set; }
    public DbSet<InstrumentInfo> InstrumentInfos { get; set; }

    public TransactionsContext()
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer<TransactionsContext>(null);
        base.OnModelCreating(modelBuilder);
    }
}

如果我运行 对其进行测试,应将 InstrumentInfo 对象添加到 DB

[TestMethod]
public void AddInstrumentInfo_Added_IsTrue()
{
    InstrumentInfo info = FakeFactory.GetInstrumentInfo();
    using (var ctx = new TransactionsContext())
    {
        ctx.InstrumentInfos.Add(info);
        ctx.SaveChanges();
    }
}

我得到以下异常:

SqlException: Cannot insert the value NULL into column 'UnifiedInstrumentCode', table 'TransactionsContext.dbo.InstrumentInfoes'; column does not allow nulls. INSERT fails. The statement has been terminated.

我尝试了我发现的所有不同场景 here 但我无法弄清楚我做错了什么。

最终目标是我以某种方式定义我的两个 类,以便 "Record" 通过 [=32= 链接到 "InstrumentInfo" table ] 属性。 我的猜测是我对这两个 table 的约束仍然不正确,但我无法弄清楚如何在 EF6 中定义它(代码优先)以使其正常工作。

注释 [DatabaseGenerated(DatabaseGeneratedOption.None)] 添加到我在 InstrumentInfo 中的主键解决了问题:

public class InstrumentInfo
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UnifiedInstrumentCode { get; set; }
    public ICollection<Record> Record { get; set; }

    public string AssetType { get; set; }
    public int UnderlyingInstrumentUic { get; set; }
    public string UnderlyingInstrumentSubType { get; set; }
    public string InstrumentSymbol { get; set; }
    public string InstrumentDescription { get; set; }
    public string InstrumentSubType { get; set; }
    public string UnderlyingInstrumentAssetType { get; set; }
    public string UnderlyingInstrumentDescription { get; set; }
    public string UnderlyingInstrumentSymbol { get; set; }
}

我没有进一步调查,但我的猜测是,如果添加了新记录,EF 最初会创建 InstrumentInfo 对象,该对象的主键具有 Null 值,这会导致异常。

希望对以后遇到同样问题的人有所帮助。