Entity framework 4 EF 4代码优先:定义导航属性
我有以下实体:Entity framework 4 EF 4代码优先:定义导航属性,entity-framework-4,ef-code-first,Entity Framework 4,Ef Code First,我有以下实体: public class Product { [Key] public int Id{get;set;} //other properties } public Coupon { [Key] public int Id {get;set;} //other properties public virtual ICollection<Product> Products { get; set; } pub
public class Product {
[Key]
public int Id{get;set;}
//other properties
}
public Coupon {
[Key]
public int Id {get;set;}
//other properties
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<CouponCode> CouponCodes { get; set; }
}
此方案正在产品表中创建优惠券Id
。实际上,我想注册给定的Coupun
有效的所有产品代码。EF解释它的方式显然是错误的,因为一个产品实体有多个有效优惠券
请帮我找出我做错了什么。如果你想建立多对多关系,你必须指示EF创建它
builder.Entity<Coupon>().HasMany(x => x.Products).WithMany();
builder.Entity();
如果您想拥有多对多关系,必须指示EF创建它
builder.Entity<Coupon>().HasMany(x => x.Products).WithMany();
builder.Entity();
public virtual ICollection优惠券{get;set;}将定义从产品到优惠券的导航属性public virtual ICollection优惠券{get;set;}将定义从产品到优惠券的导航属性