[英]1:1 mapping and Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF
I needed a 1:1
mapping of DivisionParticipant
and Team
with both IDs being auto increment. 我需要对DivisionParticipant
和Team
进行1:1
映射,两个ID均为自动递增。 I did this because we wanted to use to Nullable FK's, Team
and Player
. 我这样做是因为我们想使用Nullable FK, Team
和Player
。 Before Team ID
was the PK to DivisionParticipant ID
, but now DivisionParticipant ID
is auto increment and Team
is now added as a property with the FK to get the 1:1 working. 在Team ID
是PK之前DivisionParticipant ID
的PK之前,但现在DivisionParticipant ID
是自动递增的,并且Team
现在作为属性添加到FK中以使1:1工作。 This works fine and minimal code changes were needed when we do a read, but now on a save we get this error below. 这可以很好地工作,并且在进行读取时需要最小的代码更改,但是现在保存起来会在下面显示此错误。 The ID of DivisionParticipant
was made to autoincrement so I am unsure what column it is speaking of? DivisionParticipant
的ID是自动递增的,因此我不确定它在说什么列?
Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法为表'DivisionParticipant'中的Identity列插入显式值
Classes 类
public class DivisionParticipant
{
public int Id {get; set;}
public virtual Team Team { get; set; }
public virtual Player Player { get; set; }
}
public class Team
{
public int Id {get; set;}
public virtual DivisionParticipant DivisionParticipant { get; set; }
}
DataContext DataContext的
modelBuilder.Entity<Team>()
.HasOptional(t => t.DivisionParticipant)
.WithRequired(t => t.Team);
EF6 doesn't support 1:1 relationships without the FK on the dependent side being the PK which means the PK on one side cannot use an identity. EF6不支持1:1关系,而从属端的FK不能作为PK,这意味着一侧的PK无法使用身份。 Check out this answer for the explanation from the EF Program Manager. 从EF计划管理器中查看该答案以获得解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.