简体   繁体   English

EF 4.1 Fluent API dB第一关系映射问题

[英]EF 4.1 Fluent API dB first relationship mapping problem

I have the following tables, 我有下表

  1. Product(pro_iIDX[PK], pro_sName) 产品(pro_iIDX [PK],pro_sName)
  2. Manufacturer(man_iIDX[PK], man_sName) 制造商(man_iIDX [PK],man_sName)
  3. ProductManufacturer(pma_iIDX[PK], pma_iProductRef[FK], pma_iManufacturerRef[FK], pma_bAvailable) ProductManufacturer(pma_iIDX [PK],pma_iProductRef [FK],pma_iManufacturerRef [FK],pma_b可用)

I have the following POCOs, 我有以下POCO,

public class ProductInfo  
{  
    public int IDX { get; set; }  
    public string Name { get; set; }

    public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers 
        { get; set; }  
}  

public class ManufacturerInfo  
{  
    public int IDX { get; set; }  
    public string Name { get; set; }  

    public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers 
        { get; set; }  
}  

public class ProductManufacturerInfo  
{  
    public int IDX { get; set; }  
    public bool Available { get; set; }  

    public virtual ManufacturerInfo C0Manufacturer { get; set; }  
    public virtual ProductInfo C0ProductInfo { get; set; }  
}

I have used the following mappings without success, 我使用以下映射没有成功,

public ProductManufacturerConfiguration()  
{  
    ToTable("ProductManufacturer");  
    HasKey(p => p.IDX);  
    Property(p => p.IDX).HasColumnName("pma_iIDX");  
    Property(p => p.Available).HasColumnName("pma_bAvailable");  
    Property(p => p.ProductRef).HasColumnName("pma_iProductRef");  
    Property(p => p.ManufacturerRef).HasColumnName("pma_iManufacturerRef");  

    //I have tried  
    HasRequired(p => p.ManufacturerInfo)
            .WithMany(c => c.C0ProductManufacturers)
            .Map(m => m.MapKey("pma_iManufacturerRef"));  
    HasRequired(p => p.ProductInfo)
            .WithMany(c => c.C0ProductManufacturers)
            .Map(m => m.MapKey("pma_iProductRef"));  

    //As well as  
    HasRequired(p => p.C0Manufacturer)
            .WithMany(c => c.C0ProductManufacturers)
            .HasForeignKey(p => p.ManufacturerRef);  
    HasRequired(p => p.C0Product)
            .WithMany(c => c.C0ProductManufacturers)
            .HasForeignKey(p => p.C0Product);
}

From my trials, dB first complains about not finding ManufacturerInfo_IDX when I execute the following, 根据我的试验,dB首先抱怨在执行以下命令时找不到ManufacturerInfo_IDX

var query = from p in _context.Product  
    select p;

If I go the code first route, the following table is created, 如果我先执行代码,则会创建下表,

ProductManufacturer(
            pma_iIDX[PK], 
            pma_iProductRef, 
            pma_iManufacturerRef, 
            pma_bAvailable, 
            ManufacturerInfo_IDX, 
            ProductInfo_IDX)

Any assistance will be highly appreciated. 任何帮助将不胜感激。

I hardly believe that sample you provided is your real code because it even doesn't compile. 我几乎不相信您提供的示例就是您的真实代码,因为它甚至无法编译。 Is it so hard to copy a real code to show a problem? 复制真实代码以显示问题是否如此困难?

This works: 这有效:

public class ProductInfo
{
    public int IDX { get; set; }
    public string Name { get; set; }

    public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers
    { get; set; }
}

public class ManufacturerInfo
{
    public int IDX { get; set; }
    public string Name { get; set; }

    public virtual ICollection<ProductManufacturerInfo> C0ProductManufacturers
    { get; set; }
}

public class ProductManufacturerInfo
{
    public int IDX { get; set; }
    public bool Available { get; set; }

    public int ManufacturerRef { get; set; }        
    public virtual ManufacturerInfo C0Manufacturer { get; set; }

    public int ProductRef { get; set; }
    public virtual ProductInfo C0ProductInfo { get; set; }
}

public class ProductManufacturerConfiguration : EntityTypeConfiguration<ProductManufacturerInfo>
{
    public ProductManufacturerConfiguration()  
    {  
        ToTable("ProductManufacturer");  
        HasKey(p => p.IDX);  
        Property(p => p.IDX).HasColumnName("pma_iIDX");  
        Property(p => p.Available).HasColumnName("pma_bAvailable");
        Property(p => p.ProductRef).HasColumnName("pma_iProductRef");
        Property(p => p.ManufacturerRef).HasColumnName("pma_iManufacturerRef");  

        //I have tried  
        HasRequired(p => p.C0Manufacturer)
                .WithMany(c => c.C0ProductManufacturers)
                .Map(m => m.MapKey("pma_iManufacturerRef"));
        HasRequired(p => p.C0ProductInfo)
                .WithMany(c => c.C0ProductManufacturers)
                .Map(m => m.MapKey("pma_iProductRef"));  

        //As well as  
        HasRequired(p => p.C0Manufacturer)
                .WithMany(c => c.C0ProductManufacturers)
                .HasForeignKey(p => p.ManufacturerRef);  
        HasRequired(p => p.C0ProductInfo)
                .WithMany(c => c.C0ProductManufacturers)
                .HasForeignKey(p => p.ProductRef);
    }
}

The main problem is that your key for ProductManufacturerInfo should not really be IDX. 主要问题在于,ProductManufacturerInfo的密钥实际上不应为IDX。 IDX is more of a "payload" in your many-to-many association. IDX在您的多对多关联中更像是“有效负载”。 One way to fix this is to specify a true key and then the mapping is easy: 解决此问题的一种方法是指定一个真键,然后映射很容易:

public class ProductManufacturerInfo
{
    public int IDX { get; set; }
    public bool Available { get; set; }

    public int C0ManufacturerIDX { get; set; }
    public virtual ManufacturerInfo C0Manufacturer { get; set; }

    public int C0ProductInfoIDX { get; set; }
    public virtual ProductInfo C0ProductInfo { get; set; }
}

Then your mapping: 然后您的映射:

public class ProductManufacturerConfiguration 
    : EntityTypeConfiguration<ProductManufacturerInfo>
{
    public ProductManufacturerConfiguration()
    {
        ToTable("ProductManufacturer");
        HasKey(p => new { p.C0ManufacturerIDX, p.C0ProductInfoIDX });
        Property(p => p.IDX)
           .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    }
}

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

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