简体   繁体   English

IDENTITY_INSERT设置为OFF时,1:1映射并且无法为表'DivisionParticipant'中的Identity列插入显式值

[英]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. 我需要对DivisionParticipantTeam进行1:1映射,两个ID均为自动递增。 I did this because we wanted to use to Nullable FK's, Team and Player . 我这样做是因为我们想使用Nullable FK, TeamPlayer 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.

相关问题 SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法为表 [表名] 中的标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table [Table name] when IDENTITY_INSERT is set to OFF 出现错误:当IDENTITY_INSERT设置为OFF时,无法在表&#39;Table&#39;中为标识列插入显式值 - Getting Error: Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF 实体框架:当IDENTITY_INSERT设置为OFF时,无法为表&#39;[table]&#39;中的标识列插入显式值 - Entity Framework: Cannot insert explicit value for identity column in table '[table]' when IDENTITY_INSERT is set to OFF IDENTITY_INSERT设置为OFF时,EF无法为表“ PATIENT DONOR”中的标识列插入显式值 - EF Cannot insert explicit value for identity column in table 'PATIENT DONOR' when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,C#无法为表“出租”中的标识列插入显式值 - C# Cannot insert explicit value for identity column in table “Rentals” when IDENTITY_INSERT is set to OFF 当IDENTITY_INSERT设置为OFF时,无法在TABLE中为Identity列插入显式值; .NET Core 2.1 - Cannot insert explicit value for identity column in TABLE when IDENTITY_INSERT is set to OFF; .NET Core 2.1 当IDENTITY_INSERT设置为OFF时,无法在表'ClientDetails'中为identity列插入显式值 - Cannot insert explicit value for identity column in table 'ClientDetails' when IDENTITY_INSERT is set to OFF “当IDENTITY_INSERT设置为OFF”时,无法使用复合键为表中的标识列插入显式值 - “Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF” with composite key 当 IDENTITY_INSERT 设置为 OFF 时,无法在表“消息”中插入标识列的显式值 - Cannot insert explicit value for identity column in table 'Messages' when IDENTITY_INSERT is set to OFF SqlException:当 IDENTITY_INSERT 设置为 OFF 时,无法在表 'Ogretmenler' 中为标识列插入显式值 - SqlException: Cannot insert explicit value for identity column in table 'Ogretmenler' when IDENTITY_INSERT is set to OFF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM