![](/img/trans.png)
[英]Entity Framework error: Cannot insert explicit value for identity column in table
[英]Entity Framework 'Cannot insert explicit value for identity column' error
我理解錯誤消息在說什么。 我只是不確定為什么要這么說。 我的表具有一個Identity,並將其設置為自動遞增,但是當我去保存一個實體時,出現以下錯誤:
當IDENTITY_INSERT設置為OFF時,無法為表“會議”中的標識列插入顯式值。
我使用的是代碼優先的方法,我的Conference
實體看起來像這樣:
public class Conference
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int DugoutId { get; set; }
public int ConferenceDetailsId { get; set; }
[ForeignKey("Id")]
public virtual Dugout Dugout { get; set; }
[ForeignKey("Id")]
public virtual ConferenceDetail ConferenceDetail { get; set; }
}
當我要保存會議時,值如下:
Id: 0
DugoutId: 15
ConferenceDetailsId: 1
Dugout: null
ConferenceDetail: null
有什么想法為什么不能節省?
[ForeignKey]服裝指向Dugout和ConferenceDetails的錯誤屬性。 應該是ForeignKey(“ DugoutId”)和ForeignKey(“ ConferenceDetailsId”)。
您是先使用代碼還是正在自動生成代碼? 根據您的操作,模型和數據庫之間可能會不匹配。 您可以嘗試使用探查器確切地知道EF試圖做什么。 該線程也可以為您提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.