0

I have the following tables,

  1. Product(pro_iIDX[PK], pro_sName)
  2. Manufacturer(man_iIDX[PK], man_sName)
  3. ProductManufacturer(pma_iIDX[PK], pma_iProductRef[FK], pma_iManufacturerRef[FK], pma_bAvailable)

I have the following POCOs,

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,

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.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 1
    What code did you actually execute when you had the exception? Your `ProductManufacturerConfiguration` can't even be compiled with the POCO classes you provided above. Could you edit your question to make clear what's exactly the code which didn't work? – Slauma Apr 09 '11 at 15:58

2 Answers2

1

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);
    }
}
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
0

The main problem is that your key for ProductManufacturerInfo should not really be IDX. IDX is more of a "payload" in your many-to-many association. 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);
    }
}
anon
  • 4,578
  • 3
  • 35
  • 54