Entity Framework Core - Error inserting many-to-many relation

c# entity-framework-core

Question

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. 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.".

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.

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);
}
1
0
1/15/2018 1:45:06 PM

Popular Answer

As GertArnold said, some properties were missing. 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; }
}
0
1/15/2018 3:02:21 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow