0

所以我有以下 EF 模型:

[Table("Site")]
public class Store{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [ForeignKey("ID")]
    public virtual ICollection<Item> Items { get; set; }
}
[Table("Item")]
public class Item {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Column("SiteID")]
    public int storeID { get; set; }
    [ForeignKey("storeID")]
    public Store store { get; set; }
}

因此,从 Item->Store 开始似乎很好,数据填充。但是 Store->Items 没有填充。我究竟做错了什么?

4

1 回答 1

0

弄清楚,要建立一对多的关系,您需要使用 InverseProperty。像这样!

[Table("Site")]
public class Store {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [InverseProperty("store")]
    public virtual ICollection<Item> Items { get; set; }
}

[Table("Item")]
public class Item {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Column("SiteID")]
    public int storeID { get; set; }
    [ForeignKey("storeID")]
    public Store store { get; set; }
}

一切都加载,事件项目集合。

于 2013-05-10T19:16:04.627 回答