entity framework 4 - How to eager load child entities using repository pattern -


I have a unit named tour , which has many agents May be. I am capable of adding agents, but I can not remove them.

  // _repo is injected .... var tour = _repo.GetById (tourId); Tour.AddAgent (new agent (tour.TorId));   

When I try to call tour. The RemoveAgent () method is actually deleted, I have set a breakpoint inside the tour. RemoveAgent () Method I think the _agents property contains 0 . tour.RemoveAgent (agentId); // This does not work because _agents are empty

Do I have to do something special to populate the property _agents from the tour my repository

Update: Resolve the problem (thanks to Paul's answer)

I just decided to make a repository unique to each total, that way It's easy to define what is the need to use the include () function The Yk. This is an example where I inherited from GenericRepository & LT; T & gt; class (which is also included in the lower part of this question)

  public class TourRepository:. GenericRepository & LT; Tour & gt; {Public TourRepository (IDatabaseFactory databaseFactory): base (databaseFactory) {} public override tour GetById (GUID ID) {return dataContext.Tours .clude (x => x.Agents) single (x => x.TourId = = ID); }}   

tour class

  public partial tour tour {public guide tour id {get; Private set; } Secure Virtual List & lt; Agent & gt; _Getts {get; Set; } Public Travel () {TourID = Good Newguide (); _agents = new list & lt; Agent & gt; (); } Public Zero AddAgent (agent agent) {_agents.Add (agent); } Public Zero Exit Agent (Guide Agent ID) {_agents.RemoveAll (a => a.AgentId == Agent ID); }}   

agent class

  public partial class agent {public guide agent} {get; Private set; } Get Public Guide Tour ID {Get; Private set; } Get the public tour tour; Private set; } Public Agent (Guid tourId) {TourId = tourId; Agent = Guid.NewGuid (); }}   

OnModelCreating

  Protected Override Zero OnModelCreating (ModelBuilder modelBuilder) {// agent ======= ===================== modelBuilder.Entity & LT; Agent & gt; () .HasKey (x = & gt; x.AgentId) .Property (P => p.AgentId); Modlbilder Anti & lt; Agent & gt; () .HasRequired (p = & gt; p.Tour). Many (t = & gt; t. Agents); // Tours ============================= modelBuilder.Entity & LT; Tour & gt; () .HasKey (x = & gt; x.TourId). Property (x = & gt; x.TourId); }   

Repository class

  public class generic repository & lt; T & gt; : IRAPSorgetry & Lt; T & gt; Where T: Class {Private MyContext Datacontex; Private Readonly IDbSet & lt; T & gt; Dbset; Public Generic Resource (IDBase Database Databases) {DatabaseFactory = databaseFactory; Dbset = DataContext.Set & lt; T & gt; (); } Protected IDatabaseFactory DatabaseFactory {get; Private set; } Protected MyContext DataContext {get {return dataContext ?? (Datacentext = DatabaseFit. Gate ()); }} // ... The stuff was removed for a brief ... Public T GetById (Guide ID) {return dbset.Find (id); }}    

Try to create a protected virtual list _gents {get; Set; } Public Public Virtual List & lt; Agent & gt; _Getts {get; Set; }

You can also load anything curious by doing this:

  _databaseContext.Tours.Include (x = & gt; x.Agents). Single (X = & gt; x.TourId == tourId)   

You can also read more here:

Comments