包含不包括链接实体的实体的存储库

本文关键字:实体 存储 不包括 包含 链接 | 更新日期: 2023-09-27 18:17:20

我正在尝试以下内容。我有一个存储库,它返回存款列表。我还想获得与存款实体(即合同)相关的实体

我没有完成它。有人能帮我吗?

这是我的存储库:

public class DepositRepository : Repository<Deposit, int>, IDepositRepository
{
    public DepositRepository(IComPostSession session) : base(session) { }
    public Deposit GetById(int id)
    {
        return this.Query.SingleOrDefault(deposit => deposit.Id == id);
    }
    public IEnumerable<Deposit> GetAllOpenDeposits()
    {
        IEnumerable<Deposit> deposits = this.Query.ToList();
        return deposits;
    }
}

My Deposit-entity如下:

public class Deposit : IEntity<int>
{
    public int Id { get; set; }
    //public string Name { get; set; }
    public DateTime DepositDate { get; set; }
    public int EnvelopeTypeCarrierClassificationId { get; set; }
    public int CarrierCustomerContractVersionId { get; set; }
    public EnvelopeTypeCarrierClassification EnvelopeTypeCarrierClassificiation { get; set; }
    public CarrierCustomerContractVersion CarrierCustomerContractVerision { get; set; }
}

因此,当我得到我的存款列表时,我还需要链接的enveletypecarrierclassification和链接的CarrierCustomerContractVersion

包含不包括链接实体的实体的存储库

Include会帮助你的。

GetById更改为

public Deposit GetById(int id)
{
    return this.Query
               .Include(p => p.EnvelopeTypeCarrierClassificiation)
               .Include(p => p.CarrierCustomerContractVersion)
               .SingleOrDefault(deposit => deposit.Id == id);
}

有两种可能性。在你的DataContext中启用LazyLoading,它按需加载你的链接对象,或者如果你想自己做,你必须使用Include()如果EntityCollection你使用。

myDataContext.MyObjectEntities.Include("MyLinkedEntity").ToList();

ObjectQuery。包括