我有這個模型(簡體)相關的條目:實體框架中刪除,而不是設置爲null FK
public class Request
{
[Key]
public int Id { get; set; }
public virtual StructuredNote Response { get; set; }
}
而且每個StructuredNote
有List<CallSchedComp>
這樣的:
public class StructuredNote
{
public virtual List<CallSchedComp> CallSchedule { get; set; }
}
我想更新特別是Request
的CallSchedule
。問題往往是我需要完全重新生成CallSchedule
,而不是修改現有的參考CallSchedule
。當再生一個全新的List<CallSchedComp>
,明顯的指針現在是新的,因此,結束意外事件發生時,我做這樣的事情:
request.Response.CallSchedule = NewCallScheduleWithNewPointer;
_ctxt.Entry(request).State = EntityState.Modified;
是英孚設置的List<CallSchedule>
到null
代替現有CallSchedComp
S部的FKS從數據庫中刪除它們。新的CallSchedComp
有正確的FK指向Response
。我相信這是默認的EF行爲,對嗎?
我的問題:
- 有沒有一種方法,以消除在數據庫中的舊
List<CallSchedComp>
,而不是僅僅設置了外鍵爲空? - 如果不影響性能,我是否應該對此感到困擾?
希望它很清楚。謝謝!
編輯:由於每個請求:
public class CallSchedComp
{
[Key]
public int CallSchedCompId { get; set; }
[Range(typeof(DateTime), "1/1/1900", "6/6/2079")]
public DateTime ObservationDate { get; set; }
[Range(typeof(DateTime), "1/1/1900", "6/6/2079")]
public DateTime PaymentDate { get; set; }
public decimal Coupon { get; set; }
public decimal Strike { get; set; }
public OptionType OptionType { get; set; }
public CallType CallType { get; set; }
public decimal CallPrice { get; set; }
public decimal AutoCallLevel { get; set; }
public decimal UpsideStrike { get; set; }
public decimal UpsideParticipation { get; set; }
public bool ExcessParticipationOnAutoCallOnly { get; set; }
public virtual StructuredNote IncomeNote { get; set; }
[Timestamp]
public byte[] Timestamp { get; set; }
}
而且非常基本fluentAPI配置。我幾乎完全使用註釋來配置關係。
modelBuilder.Entity<CallSchedComp>().Property(x => x.AutoCallLevel).HasPrecision(18, 4);
modelBuilder.Entity<CallSchedComp>().Property(x => x.CallPrice).HasPrecision(18, 4);
modelBuilder.Entity<CallSchedComp>().Property(x => x.Coupon).HasPrecision(18, 4);
modelBuilder.Entity<CallSchedComp>().Property(x => x.Strike).HasPrecision(18, 4);
modelBuilder.Entity<CallSchedComp>().Property(x => x.UpsideParticipation).HasPrecision(18, 4);
modelBuilder.Entity<CallSchedComp>().Property(x => x.UpsideStrike).HasPrecision(18, 4);
這取決於關係的配置方式。你能否顯示CallSchedule模型(具有PK,FK /導航,數據註釋和/或流暢配置等所有基本屬性)? –