Hao*_*Hao 5 mapping entity-framework entity-framework-4.1
我有这些课程:
public class Product
{
[Key]
public virtual int ProductId { get; set; }
public virtual string ProductName { get; set; }
public virtual string Category { get; set; }
public virtual IList<ProductPricing> ProductPriceList { get; set; }
[Timestamp]
public virtual byte[] Version { get; set; }
}
public class ProductPricing
{
// no ProductId here
public virtual Product Product { get; set; }
[Key]
public virtual int ProductPricingId { get; set; }
public virtual DateTime EffectiveDate { get; set; }
public virtual decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的modelBuilder:
modelBuilder.Entity<Product>().
HasMany(x => x.ProductPriceList)
.WithRequired()
.HasForeignKey(x => x.Product);
Run Code Online (Sandbox Code Playgroud)
这是错误:
外键组件"Product"不是"ProductPricing"类型的声明属性.验证它是否未从模型中明确排除,并且它是有效的原始属性.
UPDATE
我已经尝试了以下代码下面的相应错误
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductPriceList)
.WithRequired();
Run Code Online (Sandbox Code Playgroud)
{"无效的列名'Product_ProductId1'.\ r \n无效的列名'Product_ProductId'.\ r \n无效的列名'Product_ProductId1'."}
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductPriceList)
.WithRequired()
.Map(x => x.MapKey("ProductId"));
Run Code Online (Sandbox Code Playgroud)
{"无效的列名'Product_ProductId'."}
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductPriceList)
.WithRequired(x => x.Product);
Run Code Online (Sandbox Code Playgroud)
{"无效的列名'Product_ProductId'.\ r \n无效的列名'Product_ProductId'."}
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductPriceList)
.WithRequired(x => x.Product)
.Map(x => x.MapKey("ProductId"));
Run Code Online (Sandbox Code Playgroud)
{"违反了多重约束.关系'TestEfCrud.Mappers.Product_ProductPriceList'的角色'Product_ProductPriceList_Source'具有多重性1或0..1."}
如果它可以帮助,这是DDL:
create table Product
(
ProductId int not null identity(1,1) primary key,
ProductName varchar(100) not null,
Category varchar(100) not null,
Version rowversion not null
);
create table ProductPricing
(
ProductId int not null references Product(ProductId),
ProductPricingId int identity(1,1) not null primary key,
EffectiveDate datetime not null,
Price decimal(18,6) not null
);
Run Code Online (Sandbox Code Playgroud)
更新2
我试过这个答案,看起来有点类似于我的情况,映射起源于子实体 如何映射EF 4.1代码中的父列
但是,使用这个:
modelBuilder.Entity<ProductPricing>()
.HasOptional(x => x.Product)
.WithMany()
.Map(x => x.MapKey("ForeignKeyColumn"));
Run Code Online (Sandbox Code Playgroud)
还有这个:
modelBuilder.Entity<ProductPricing>()
.HasRequired(x => x.Product)
.WithMany()
.HasForeignKey(x => x.Product);
Run Code Online (Sandbox Code Playgroud)
两者都导致了这个错误:
{"无效的列名'Product_ProductId1'.\ r \n无效的列名'Product_ProductId1'.\ r \n无效的列名'Product_ProductId1'."}
我不明白你为什么使用流畅的映射?您的模型应按默认约定进行映射。如果你想用流畅的映射来映射它,请使用:
modelBuilder.Entity<Product>()
.HasMany(x => x.ProductPriceList) // Product has many ProductPricings
.WithRequired(y => y.Product) // ProductPricing has required Product
.Map(m => m.MapKey("ProductId")); // Map FK in database to ProductId column
Run Code Online (Sandbox Code Playgroud)