我在 ASP.NET MVC 中使用实体框架 6.1。
我的模型是:
public class Article
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public double Price { get; set; }
[InverseProperty("Article")]
public virtual ICollection<FormulaItem> FormulaItem { get; set; }
}
public class FormulaItem
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
[ForeignKey("IdMaster")]
public virtual Formula Formula { get; set; }
public int IdMaster { get; set; }
[ForeignKey("IdArticle")]
public virtual Article Article { get; set; }
public int IdArticle { get; set; }
public string Comment { get; set; }
public int Count { get; set; }
}
public class Formula
{
[Key]
[Column(Order = 0)]
public int Id { get; set; }
public FormulaMode Mode { get; set; }
// Wen add this line I get error
//[ForeignKey("IdArticle")]
//public virtual Article Article { get; set; }
//public int? IdArticle { get; set; }
public string Comment { get; set; }
public virtual IList<FormulaItem> Items { get; set; }
public Formula()
{
Items = new List<FormulaItem>();
}
}
此示例工作正常,但添加新 poco 时:
// When I add this line in class formula I get error
[ForeignKey("IdArticle")]
public virtual Article Article { get; set; }
public int? IdArticle { get; set; }
上课Formula
我得到错误:
Formula_Items_Source_Formula_Items_Target: : 关系约束中的从属角色和主要角色中的属性数量必须相同