Search code examples
c#entity-frameworkrepository-patternrepository-design

Repository with entities not including linked entities


i'm trying the following. I have a repository that returns a list of Deposits. I also want to get the entities that are related to the Deposit-entity (i.e. contract)

I'm not getting it done. Can anyone help me?

This is my repository :

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 looks as follows:

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; }
}

So when i get my list of deposits, i also want the linked EnvelopeTypeCarrierClassification and the linked CarrierCustomerContractVersion


Solution

  • Include will help you.

    Change your GetById to

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