简体   繁体   中英

DbUpdateException when adding item to Entity Framework Context

I have the following two classes:

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

that I want to use as my context for EF6.

I defined the context the following way:

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

If I run a test against it that shall add and InstrumentInfo object to the DB

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

I get the following exception:

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.

I tried all different scenarios that I found here but I couldn't figure out what I'm doing wrong.

The ultimate goal is that i define my two classes in a way so that a "Record" is linked to the "InstrumentInfo" table via the "UnifiedInstrumentCode" property. My guess is that my constraints for this two tables are still not correct, but I cant figure out how to define it in EF6 (code first) to get this working.

Adding the annotation [DatabaseGenerated(DatabaseGeneratedOption.None)] to my primary key in InstrumentInfo solved the problem:

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

I did not investigate further but my guess is that if a new Record is added, EF initially creates and InstrumentInfo object that has a Null Value for its Primary key which causes the Exception.

I hope it helps if somebody runs into the same problem in future.

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