简体   繁体   English

实体框架核心-插入多对多关系时出错

[英]Entity Framework Core - Error inserting many-to-many relation

I use Entity Framework Core 2.0.1 and the goal is to insert some many-to-many related objects in the DB by an auxiliar entity. 我使用Entity Framework Core 2.0.1,目标是通过辅助实体在DB中插入一些与许多相关的对象。 However, I get the error: 但是,我得到了错误:

" The instance of entity type 'ReferenciaFabricanteTieneReferenciaConstructor' cannot be tracked because another instance with the key value 'IdReferenciaConstructor:0, IdReferenciaFabricante:0' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. ". 无法跟踪实体类型'ReferenciaFabricanteTieneReferenciaConstructor'的实例,因为已经跟踪了另一个具有键值'IdReferenciaConstructor:0,IdReferenciaFabricante:0'的实例。在附加现有实体时,请确保只有一个具有给定键值的实体实例附件。

I guess that the problem may be that in the list of join objects to be inserted, each of them have the foreign keys (ID's) as 0, but I thought EF would get rid of this generating the corresponding values for the entities ID's (if new), inserting/modifying them and finally the join one with these values updated. 我想问题可能是在要插入的连接对象列表中,每个对象的外键(ID)为0,但我认为EF会摆脱这种情况,从而为实体ID生成相应的值(如果新增),然后插入/修改它们,最后更新这些值的联接之一。

What should I change in order to achieve this many-to-many insertion? 为了实现这种多对多插入,我应该改变什么?

The classes are: 这些类是:

[Table("ReferenciasFabricante", Schema = "public")]
public class ReferenciaFabricante
{

    [Key]
    public int Id { get; set; }

    ...

    //JOIN TABLE
    public IList<ReferenciaFabricanteTieneReferenciaConstructor> ReferenciaFabricanteTieneReferenciaConstructor { get; set; }

}


[Table("ReferenciasConstructor", Schema = "public")]
public class ReferenciaConstructor
{

    [Key]
    public int Id { get; set; }

    ...

    //JOIN TABLE
    public IList<ReferenciaFabricanteTieneReferenciaConstructor> ReferenciaFabricanteTieneReferenciaConstructor { get; set; }

}


//JOIN TABLE
[Table("ReferenciaFabricanteTieneReferenciaConstructor", Schema = "public")]
public class ReferenciaFabricanteTieneReferenciaConstructor {

    [Key]
    public int IdReferenciaFabricante { get; set; }

    public ReferenciaFabricante ReferenciaFabricante { get; set; }

    [Key]
    public int IdReferenciaConstructor { get; set; }

    public ReferenciaConstructor ReferenciaConstructor { get; set; }
}

Model: 模型:

protected override void OnModelCreating(ModelBuilder modelBuilder) {

    modelBuilder.Entity<ReferenciaFabricanteTieneReferenciaConstructor>().HasKey(v => new { v.IdReferenciaFabricante, v.IdReferenciaConstructor });
}

Code: 码:

foreach (ApplicationCore.Entities.ReferenciaFabricanteTieneReferenciaConstructor referenciaFabricanteTieneReferenciaConstructor in referenciaConstructor.ReferenciaFabricanteTieneReferenciaConstructor) {

    //ERROR
    _dbContext.ReferenciaFabricanteTieneReferenciaConstructor.Add(referenciaFabricanteTieneReferenciaConstructor);
}

As GertArnold said, some properties were missing. 正如GertArnold所说,缺少某些属性。 Updated classes: 更新的类:

[Table("ReferenciasFabricante", Schema = "public")]
public class ReferenciaFabricante
{

    [Key]
    public int Id { get; set; }

    ...

    //JOIN TABLE
    [InverseProperty("ReferenciaFabricante")]
    public IList<ReferenciaFabricanteTieneReferenciaConstructor> ReferenciaFabricanteTieneReferenciaConstructor { get; set; }

}


[Table("ReferenciasConstructor", Schema = "public")]
public class ReferenciaConstructor
{

    [Key]
    public int Id { get; set; }

    ...

    //JOIN TABLE
    [InverseProperty("ReferenciaConstructor")]
    public IList<ReferenciaFabricanteTieneReferenciaConstructor> ReferenciaFabricanteTieneReferenciaConstructor { get; set; }

}


//JOIN TABLE
[Table("ReferenciaFabricanteTieneReferenciaConstructor", Schema = "public")]
public class ReferenciaFabricanteTieneReferenciaConstructor {

    [Key]
    public int IdReferenciaFabricante { get; set; }

    [ForeignKey("IdReferenciaFabricante")]
    public ReferenciaFabricante ReferenciaFabricante { get; set; }

    [Key]
    public int IdReferenciaConstructor { get; set; }

    [ForeignKey("IdReferenciaConstructor")]
    public ReferenciaConstructor ReferenciaConstructor { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM