EF Core:如何基于2个实体向对象添加导航属性

时间:2018-07-24 03:14:34

标签: asp.net-core entity-framework-core

我想知道是否有一种添加导航属性的方法,该方法将使用2个属性来加载实体,但我不确定该怎么做。

这不是一个很好的例子,但它解释了这个概念:

一个QuantityOnHand实体,它保存某个位置中某个库存项目的当前库存水平。

public class Qoh
{
    public virtual int Id { get; set; }

    [Required]
    [ForeignKey("LocationId")]
    public virtual Location Location{ get; set; }
    public virtual int LocationId { get; set; }

    [Required]
    [ForeignKey("StockId")]
    public virtual Stock Stock{ get; set; }
    public virtual int StockId { get; set; }

    public virtual double CurrentLevel{ get;  set; }
}

加载转储时,我希望转储项上具有一个导航属性,该属性将为要转储的位置加载库存的当前库存水平,即:

public class Transfer
{
    public virtual int Id { get; set; }

    public virtual int FromLocationId { get; set; }
    [ForeignKey("FromLocationId")]
    public virtual Location FromLocation { get; set; }

    public virtual int ToLocationId { get; set; }
    [ForeignKey("ToLocationId")]
    public virtual Outlet ToLocation { get; set; }

    public virtual ICollection<TransferItem> Items { get; set; }
}

public class TransferItem
{
    public virtual int Id { get; set; }

    [ForeignKey("TransferId")]
    public virtual int TransferId { get; set; }
    public virtual Transfer Transfer { get; set; }

    [ForeignKey("StockId")]
    public virtual int StockId { get; set; }
    public virtual Stock Stock{ get;  set; }

    public virtual double Quantity { get; set; }

    public virtual Qoh FromQoh { get; set; }

    public virtual Qoh ToQoh { get; set; }
}

如何定义FromQoh和ToQoh属性,以便它们从Qoh表中为Transfer项目中定义的StockId和Transfer对象中定义的关联LocationId字段加载?

非常感谢实体框架专家的任何帮助!

0 个答案:

没有答案