实体框架“无法为标识列插入显式值"错误 [英] Entity Framework 'Cannot insert explicit value for identity column' error
问题描述
我了解错误消息在说什么.我只是不确定为什么要这么说.我的表具有一个Identity,并将其设置为自动递增,但是当我去保存一个实体时,出现以下错误:
I understand what the error message is saying. I'm just not sure why it is saying it. My table has an Identity and it is set to auto-increment, yet when I go to save an entity I get the following error:
当IDENTITY_INSERT设置为OFF时,无法在表会议"中为标识列插入显式值.
Cannot insert explicit value for identity column in table 'Conferences' when IDENTITY_INSERT is set to OFF.
我使用的是代码优先的方法,我的 Conference
实体看起来像这样:
I'm using a code-first approach and my Conference
entity looks like this:
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; }
}
当我要保存会议时,值如下:
When I am going to save the Conference, the values are as follows:
Id: 0
DugoutId: 15
ConferenceDetailsId: 1
Dugout: null
ConferenceDetail: null
任何想法为何无法保存?
Any ideas why this won't save?
推荐答案
[ForeignKey]服装指向Dugout和ConferenceDetails的错误属性.应该是ForeignKey("DugoutId")和ForeignKey("ConferenceDetailsId").
The [ForeignKey] attibutes are pointing to wrong property for Dugout and ConferenceDetails. Should be ForeignKey("DugoutId") and ForeignKey("ConferenceDetailsId").
这篇关于实体框架“无法为标识列插入显式值"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!