簡體   English   中英

將項目添加到實體框架上下文時出現 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 對象添加到數據庫中

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

我收到以下異常:

SqlException: 無法將值 NULL 插入到列“UnifiedInstrumentCode”、表“TransactionsContext.dbo.InstrumentInfoes”中; 列不允許空值。 插入失敗。 該語句已終止。

我嘗試了在這里找到的所有不同場景,但我無法弄清楚我做錯了什么。

最終目標是我以某種方式定義我的兩個類,以便通過“UnifiedInstrumentCode”屬性將“Record”鏈接到“InstrumentInfo”表。 我的猜測是我對這兩個表的約束仍然不正確,但我無法弄清楚如何在 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 值,這會導致異常。

如果將來有人遇到同樣的問題,我希望它會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM