应该使用哪些属性进行正确关联

时间:2014-11-08 17:50:05

标签: c# entity-framework

我使用实体框架并拥有一些类

public class Lot
{
    public int LotId { get; set; }
    public string Description { get; set; }
    public virtual Product Product { get; set; }
    public virtual ApplicationUser Owner { get; set; }
    public virtual ApplicationUser CurrentCustomer { get; set; }
    public virtual ICollection<ApplicationUser> AllCustomers { get; set; }
    public virtual AuctionDates AuctionDates { get; set; }
    public  virtual AuctionPrices AuctionPrices { get; set; }
    public virtual State State { get; set; }
    public virtual ProductImages Images { get; set; }
}

public class ApplicationUser : IdentityUser
{
    [Required]
    public virtual UserInfo UserInfo { get; set; }
    public virtual ICollection<Lot> SelledLots { get; set; }
    public virtual ICollection<Lot> BuyedLots { get; set; }
    public virtual ICollection<Lot> ParticipatedLots { get; set; }
}

问题是我如何设置在所有者(Lot)中出售批次(ApplicationUser)的映射用户的配置,在当前客户(Lot)中购买批次(ApplicationUser)等等。非常感谢。

1 个答案:

答案 0 :(得分:0)

我终于找到了答案。在这种情况下,应使用数据注释InverseProperty。 https://www.safaribooksonline.com/library/view/programming-entity-framework/9781449317867/ch04s03.html

public class Lot
{
    public int LotId { get; set; }
    public string Description { get; set; }
    public virtual Product Product { get; set; }
    [InverseProperty("SelledLots")]
    public virtual ApplicationUser Owner { get; set; }
    [InverseProperty("BuyedLots")]
    public virtual ApplicationUser CurrentCustomer { get; set; }
    [InverseProperty("ParticipatedLots")]
    public virtual ICollection<ApplicationUser> AllCustomers { get; set; }
    public virtual AuctionDates AuctionDates { get; set; }
    public  virtual AuctionPrices AuctionPrices { get; set; }
    public virtual State State { get; set; }
    public virtual ProductImages Images { get; set; }
}