【发布时间】:2011-04-09 01:01:20
【问题描述】:
我有以下表格,
- 产品(pro_iIDX[PK], pro_sName)
- 制造商(man_iIDX[PK], man_sName)
- ProductManufacturer(pma_iIDX[PK], pma_iProductRef[FK], pma_iManufacturerRef[FK], pma_bAvailable)
我有以下 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)
我们将不胜感激。
【问题讨论】:
-
发生异常时您实际执行了哪些代码?您的
ProductManufacturerConfiguration甚至无法使用您在上面提供的 POCO 类进行编译。您能否编辑您的问题以明确哪些代码不起作用?
标签: c# entity-framework-4.1 fluent-interface database-first