C# EF 4.1 Fluent API dB第一关系映射问题
我有以下表格C# EF 4.1 Fluent API dB第一关系映射问题,c#,entity-framework-4.1,fluent-interface,database-first,C#,Entity Framework 4.1,Fluent Interface,Database First,我有以下表格 产品(pro_iIDX[PK],pro_sName) 制造商(man_iIDX[PK],man_sName) 产品制造商(pma_iIDX[PK]、pma_IPProductRef[FK]、pma_iManufacturerRef[FK]、pma_BaAvailable) 我有以下POCO public class ProductInfo { public int IDX { get; set; } public string Name { get; s
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; }
}
从我的试验中,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)
非常感谢您的帮助。主要问题是,ProductManufactureInfo的密钥实际上不应该是IDX。IDX在多对多关联中更像是一个“有效负载”。解决此问题的一种方法是指定一个true键,这样映射就很容易了:
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);
}
}
公共类产品制造商配置
:EntityTypeConfiguration
{
公共产品制造商配置()
{
ToTable(“产品制造商”);
HasKey(p=>new{p.C0ManufacturerIDX,p.C0ProductInfoIDX});
属性(p=>p.IDX)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
}
我几乎不相信您提供的示例是您真正的代码,因为它甚至没有编译。复制一个真实的代码来显示问题有那么难吗
这项工作:
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);
}
}
公共类ProductInfo
{
公共int IDX{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection产品制造商
{get;set;}
}
公共类制造商信息
{
公共int IDX{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection产品制造商
{get;set;}
}
公共类产品制造商信息
{
公共int IDX{get;set;}
公共bool可用{get;set;}
public int-ManufacturerRef{get;set;}
公共虚拟制造商信息C0制造商{get;set;}
public int ProductRef{get;set;}
公共虚拟ProductInfo C0ProductInfo{get;set;}
}
公共类ProductManufacturerConfiguration:EntityTypeConfiguration
{
公共产品制造商配置()
{
ToTable(“产品制造商”);
HasKey(p=>p.IDX);
属性(p=>p.IDX);
属性(p=>p.Available);
属性(p=>p.ProductRef).HasColumnName(“pma_iProductRef”);
属性(p=>p.ManufacturerRef).HasColumnName(“pma_iManufacturerRef”);
//我试过了
需要(p=>p.C0制造商)
.有许多(c=>c.C0产品制造商)
.Map(m=>m.MapKey(“pma_imanufactorref”);
HasRequired(p=>p.C0ProductInfo)
.有许多(c=>c.C0产品制造商)
.Map(m=>m.MapKey(“pma_iProductRef”);
//以及
需要(p=>p.C0制造商)
.有许多(c=>c.C0产品制造商)
.HasForeignKey(p=>p.ManufacturerRef);
HasRequired(p=>p.C0ProductInfo)
.有许多(c=>c.C0产品制造商)
.HasForeignKey(p=>p.ProductRef);
}
}
发生异常时,您实际执行的代码是什么?您的ProductManufacturerConfiguration
甚至无法使用上面提供的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 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);
}
}