1:1 映射和当 IDENTITY_INSERT 设置为 OFF 时无法在 table 'DivisionParticipant' 中为标识列插入显式值
1:1 mapping and Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF
我需要 DivisionParticipant
和 Team
的 1:1
映射,两个 ID 都是自动递增的。我这样做是因为我们想使用 Nullable FK,Team
和 Player
。之前 Team ID
是 DivisionParticipant ID
的 PK,但现在 DivisionParticipant ID
是自动递增,现在 Team
添加为 属性 与 FK 以获得 1:1 工作。这工作正常,当我们进行读取时需要最少的代码更改,但现在在保存时我们会在下面收到此错误。 DivisionParticipant
的 ID 是自动递增的,所以我不确定它在说什么列?
Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF
类
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
modelBuilder.Entity<Team>()
.HasOptional(t => t.DivisionParticipant)
.WithRequired(t => t.Team);
EF6 不支持 1:1 依赖方的 FK 不是 PK 的关系,这意味着一方的 PK 不能使用身份。查看 EF 项目经理的解释。
我需要 DivisionParticipant
和 Team
的 1:1
映射,两个 ID 都是自动递增的。我这样做是因为我们想使用 Nullable FK,Team
和 Player
。之前 Team ID
是 DivisionParticipant ID
的 PK,但现在 DivisionParticipant ID
是自动递增,现在 Team
添加为 属性 与 FK 以获得 1:1 工作。这工作正常,当我们进行读取时需要最少的代码更改,但现在在保存时我们会在下面收到此错误。 DivisionParticipant
的 ID 是自动递增的,所以我不确定它在说什么列?
Cannot insert explicit value for identity column in table 'DivisionParticipant' when IDENTITY_INSERT is set to OFF
类
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
modelBuilder.Entity<Team>()
.HasOptional(t => t.DivisionParticipant)
.WithRequired(t => t.Team);
EF6 不支持 1:1 依赖方的 FK 不是 PK 的关系,这意味着一方的 PK 不能使用身份。查看