1

I have the following class:

public class Test
{
    public Test()
    {
        this.TestQuestions = new List<TestQuestion>();
    }
    public int TestId { get; set; }
    public string Title { get; set; }
    public virtual ICollection<TestQuestion> TestQuestions { get; set; }
}

In my repository:

public virtual T GetById(int id)
{
    return DbSet.Find(id);
}

I want to get an entity by giving it the id so I am doing this in my service:

public Test GetTest(int testId)
{
    var test = _testsRepository
            .GetById(testId);
   return test;
}

However the Test entity has a collection and it's not including the collection.

Can someone tell me how I can make it so the call to the repository allows me to also as for the collection? What I am hoping for is to make it so in the service I have a .Include and a LINQ query.

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • So you have a repository that wraps a DbSet, a service that wraps a repository and then a method in a class that wraps a service... Are you willing to reconsider this and to flatten the architecture so you can access DbSet (more) directly in `GetTest`? – Gert Arnold Feb 05 '14 at 21:18
  • I'd like to be able to issue a LINQ type query in my service if possible that has a .Include – Samantha J T Star Feb 06 '14 at 04:33
  • DbSet is an implementation of the repository pattern. What are you trying to achieve by wrapping this in your own repository? Why not use the DbSet directly from the service? – Martin Booth Feb 06 '14 at 04:55
  • @MartinBooth - Can you give me an example of how I could do this? – Samantha J T Star Feb 06 '14 at 05:24
  • Instead of calling the _testsRespository.GetById method in the service, put the code you wrote in the _testsRespository.GetById (i.e. DbSet.Find(id);) into the service. That way you can use features of entity framework (such as the include method) without having to find away to map them through a repository – Martin Booth Feb 06 '14 at 05:45
  • @MartinBooth - Hi Martin, I appreciate your suggestions but I think this is getting away from the original question. – Samantha J T Star Feb 06 '14 at 07:39
  • The point is, of course you can parametrize includes, but you have to dig through a few layers to get the parameters where they they finally have effect. Same discussion here: http://stackoverflow.com/questions/19393748/entity-framework-5-is-it-possible-to-load-relationships-associations-without#comment29110680_19393748 – Gert Arnold Feb 06 '14 at 07:53

0 Answers0