2011-04-09 98 views
0

我有以下表,EF 4.1流利API分貝第一關係映射問題

  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)

我有以下波蘇斯,

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) 

任何援助將是非常的應用reciated。

+1

什麼代碼,當你有例外,你真正執行?你的'ProductManufacturerConfiguration'甚至不能用你上面提供的POCO類來編譯。你能編輯你的問題,以明確什麼是沒有工作的代碼? – Slauma 2011-04-09 15:58:48

回答

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