簡體   English   中英

EF 4.1 Fluent API dB第一關系映射問題

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

我有下表

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

我有以下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)

任何幫助將不勝感激。

我幾乎不相信您提供的示例就是您的真實代碼,因為它甚至無法編譯。 復制真實代碼以顯示問題是否如此困難?

這有效:

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

主要問題在於,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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM