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

时间:2011-04-09 01:01:21

标签: c# entity-framework-4.1 fluent-interface database-first

我有以下表格,

  1. 产品(pro_iIDX [PK],pro_sName)
  2. 制造商(man_iIDX [PK],man_sName)
  3. ProductManufacturer(pma_iIDX [PK],pma_iProductRef [FK],pma_iManufacturerRef [FK],pma_bAvailable)
  4. 我有以下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; }  
    }
    

    我使用了以下映射但没有成功,

    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);
    }
    

    根据我的试验,当我执行以下操作时,dB首先抱怨没有找到ManufacturerInfo_IDX

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

    如果我去代码第一条路线,则会创建下表

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

    任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

我几乎不相信你提供的样本是真正的代码,因为它甚至无法编译。复制真实代码以显示问题是否如此困难?

这有效:

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);
    }
}

答案 1 :(得分:0)

主要问题是您的ProductManufacturerInfo密钥不应该是IDX。 IDX在您的多对多关联中更像是“有效负载”。解决此问题的一种方法是指定一个真正的密钥,然后映射很简单:

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; }
}

然后你的映射:

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