简体   繁体   中英

Map one to many composite key Entity framework 5

i am mapping my entities but when i try to get just get back the entity but no the relationship example i have the entity called Medicamento who has many definitions but just get the properties of medicamento and no the relationship whit definitions.

i my third table i have a composite key my code is:

MedicamentoMap

public class MedicamentoMap : EntityTypeConfiguration<Medicamento>
{
    public MedicamentoMap()
    {
        // primaryKey
        this.HasKey(m => m.Id);

        // propiedades
        this.ToTable("Medicamento");
        this.Property(m => m.Id).HasColumnName("MedicamentoID");
        this.Property(m => m.Descripcion).HasColumnName("Descripcion");

        //Relaciones        
        this.HasMany(p => p.Presentaciones) 
            .WithMany() 
            .Map(m => 
                { 
                    m.ToTable("MedicamentoPresentacion")
                    m.MapLeftKey("MedicamentoID"); 
                    m.MapRightKey( "PresentacionID"); 
                });  
     }
}

the entitie definition is:

public PresentacionMap()
{
    // primaryKey
    this.HasKey(m => m.Id);

    // propiedades
    this.ToTable("Presentacion");
    this.Property(m => m.Id).HasColumnName("PresentacionID");
    this.Property(m => m.Descripcion).HasColumnName("Descripcion"); 
}

and finally the third entity is medicamentoPresentacion is:

public MedicamentoPresentacionMap()
{
    // primaryKey
    this.HasKey(i => new {i.MedicamentoID, i.PresentacionID});
    this.Property(i => i.CodigodeBarras);
    this.ToTable("MedicamentoPresentacion");
    this.Property(i => i.MedicamentoID).HasColumnName("MedicamentoID");
    this.Property(i => i.PresentacionID).HasColumnName("PresentacionID");
    this.Property(i => i.CodigodeBarras).HasColumnName("CodigoBarras");
}

You can't have both the many-to-many definition ( HasMany...WithMany ) and a visible junction class in the class model ( MedicamentoPresentacion ).

You have to remove the many-to-many mapping, because your junction table is not a pure junction table (which is a table with nothing but two foreign keys). Your junction class also contains a barcode (I think), so it has a meaning in the business domain and it should be part of the model.

So remove the HasMany...WithMany part and hang on to MedicamentoPresentacionMap . Both Medicamento and Presentacion should have a collection property MedicamentoPresentacions . (Which makes this a 1-n-1 association).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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