Entity framework 主键冲突,尝试插入外键表

Entity framework 主键冲突,尝试插入外键表,entity-framework,ef-core-2.0,Entity Framework,Ef Core 2.0,我尝试将新记录添加到规则表中。在插入规则表时,我发现一个错误,就像它试图插入到它的关系表中一样。我以前也做过类似的插入操作,但我从未遇到过这种错误。我也搜索并尝试了其他用户的建议,但对我无效 public class RuleDomainModel { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)]//tried public int ObjectID { get; set; } public st

我尝试将新记录添加到规则表中。在插入规则表时,我发现一个错误,就像它试图插入到它的关系表中一样。我以前也做过类似的插入操作,但我从未遇到过这种错误。我也搜索并尝试了其他用户的建议,但对我无效

 public class RuleDomainModel
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]//tried
    public int ObjectID { get; set; }
    public string Name { get; set; }
    //[Key] //tried
    public int? BranchID { get; set; }

    [ForeignKey("BranchID")]//tried
    public virtual BranchDomainModel Branch { get; set; }
}

public class BranchDomainModel : BaseModel
{
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]//tried
    [Key]
    public int ObjectID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<UnitDomainModel> Units { get; set; }
    [ForeignKey("BranchID")]//tried
    public virtual ICollection<RuleDomainModel> Rules { get; set; }
}
公共类RuleDomainModel
{
[关键]
[DatabaseGenerated(DatabaseGeneratedOption.None)]//已尝试
public int ObjectID{get;set;}
公共字符串名称{get;set;}
//[键]//已尝试
公共int?BranchID{get;set;}
[ForeignKey(“BranchID”)]//已尝试
公共虚拟分支DomainModel分支{get;set;}
}
公共类BranchDomainModel:BaseModel
{
[必需]
[DatabaseGenerated(DatabaseGeneratedOption.None)]//已尝试
[关键]
public int ObjectID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection单元{get;set;}
[ForeignKey(“BranchID”)]//已尝试
公共虚拟ICollection规则{get;set;}
}
类映射

public void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<RuleDomainModel>().ToTable("Rules");
        builder.Entity<RuleDomainModel>().HasKey(t => t.ObjectID);
        builder.Entity<RuleDomainModel>().Property(t =>t.Name);
        builder.Entity<RuleDomainModel>().HasOne(t => t.Branch).WithMany(t => t.Rules).HasForeignKey(t => t.BranchID).HasPrincipalKey(t => t.ObjectID);
    }

 public void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<BranchDomainModel>().ToTable("Branch");
        builder.Entity<BranchDomainModel>().HasKey(t => t.ObjectID);
        builder.Entity<BranchDomainModel>().Property(t => t.Name);
        builder.Entity<BranchDomainModel>().HasMany(m => m.Units).WithOne(x => x.Branch).HasForeignKey(x => x.xxxID).HasPrincipalKey(m => m.ObjectID);
    }
public void OnModelCreating(ModelBuilder)
{
builder.Entity().ToTable(“规则”);
builder.Entity().HasKey(t=>t.ObjectID);
builder.Entity().Property(t=>t.Name);
builder.Entity().HasOne(t=>t.Branch).WithMany(t=>t.Rules).HasForeignKey(t=>t.BranchID.HasPrincipalKey(t=>t.ObjectID);
}
模型创建时的公共无效(ModelBuilder)
{
builder.Entity().ToTable(“分支机构”);
builder.Entity().HasKey(t=>t.ObjectID);
builder.Entity().Property(t=>t.Name);
builder.Entity();
}
这是引发异常的一点:

if (model.ObjectID == 0)
{
    RuleDomainModel mod = Mapper.Map<RuleDomainModel>(model);
    uow.Repository<RuleDomainModel>().Add(mod);
    uow.Commit(requestOwnerModel); // Cannot insert duplicate key error
}
else
 {
    var mod = Mapper.Map<RuleDomainModel>(model);
    uow.Repository<RuleDomainModel>().Update(mod);
    uow.Commit(requestOwnerModel);
 }
if(model.ObjectID==0)
{
RuleDomainModel mod=Mapper.Map(模型);
uow.Repository().Add(mod);
Commit(requestOwnerModel);//无法插入重复的密钥错误
}
其他的
{
var mod=Mapper.Map(模型);
uow.Repository().Update(mod);
提交(requestOwnerModel);
}

我希望有人能帮忙,谢谢

在映射到域模型后,我通过添加
model.Branch=null
解决了我的问题。但我不知道这是最好的方法。如果有人知道,请写在这里。谢谢。

我通过在映射到域模型后添加
model.Branch=null
解决了我的问题。但我不知道这是最好的方法。如果有人知道,请写在这里。谢谢。

在添加
模型之前,将
模型.Branch
附加到上下文中。在添加
模型之前,将
模型.Branch
附加到上下文中。