简体   繁体   English

映射一个到多个复合密钥实体框架5

[英]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. 我正在映射我的实体,但是当我试图回到实体而没有关系的例子时,我有一个名为Medicamento的实体,它有很多定义,但只是得到药剂的属性而没有关系白衣定义。

i my third table i have a composite key my code is: 我的第三个表我有一个复合键我的代码是:

MedicamentoMap 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 ). 您不能同时拥有多对多定义( HasMany...WithMany )和类模型中的可见联结类( 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 . 所以删除HasMany...WithMany部分,并挂在MedicamentoPresentacionMap Both Medicamento and Presentacion should have a collection property MedicamentoPresentacions . MedicamentoPresentacion都应该有一个收集属性MedicamentoPresentacions (Which makes this a 1-n-1 association). (这使得它成为1-n-1关联)。

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

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