DbUpdateException when adding item to Entity Framework Context

c# constraints entity-framework entity-framework-6

Question

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.

1
0
3/24/2020 7:20:45 AM

Accepted Answer

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.

0
3/26/2020 4:33:34 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow