Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4094830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:48:01+00:00 2026-05-20T19:48:01+00:00

I have an entity named Tour which can have many Agents . I am

  • 0

I have an entity named Tour which can have many Agents. I am able to add agents, but I cannot remove them.

// _repo is injected....
var tour = _repo.GetById(tourId);
tour.AddAgent(new Agent(tour.TourId));

When I attempt to call the Tour.RemoveAgent() method nothing is actually removed. I set a breakpoint inside the Tour.RemoveAgent() method I see that the _agents property has a count of 0.

tour.RemoveAgent(agentId); // This doesn't work because _agents is empty

Do I have to do something special for EF to populate the _agents property when I retrieve the Tour from my repository?

UPDATE: PROBLEM SOLVED (thanks to Paul’s answer)

I decided to just create a Repository unique to each aggregate, that way it is easy to define exactly what needs to be included using the Include() function. This is an example where I inherit from the GenericRepository<T> class (which is also included at the bottom of this question).

public class TourRepository : GenericRepository<Tour>
{
    public TourRepository(IDatabaseFactory databaseFactory) : base (databaseFactory)
    {
    }

    public override Tour GetById(Guid id)
    {
        return dataContext.Tours
                .Include(x => x.Agents)
                .Single(x => x.TourId == id);
    }
}

Tour Class

public partial class Tour
{
  public Guid TourId { get; private set; }
  protected virtual List<Agent> _agents { get; set; }

  public Tour()
  {
    TourId = Guid.NewGuid();
    _agents = new List<Agent>();
  }

  public void AddAgent(Agent agent)
  {
    _agents.Add(agent);
  }

  public void RemoveAgent(Guid agentId)
  {
    _agents.RemoveAll(a => a.AgentId == agentId);
  }
}

Agent Class

public partial class Agent
{
  public Guid AgentId { get; private set; }
  public Guid TourId { get; private set; }
  public Tour Tour { get; private set; }

  public Agent(Guid tourId)
  {
    TourId = tourId;
    AgentId = Guid.NewGuid();
  }
}

OnModelCreating

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // AGENTS ============================
  modelBuilder.Entity<Agent>()
              .HasKey(x => x.AgentId)
              .Property(p => p.AgentId);

  modelBuilder.Entity<Agent>()
              .HasRequired(p => p.Tour)
              .WithMany(t => t.Agents);

  // TOURS =============================
  modelBuilder.Entity<Tour>()
              .HasKey(x => x.TourId)
              .Property(x => x.TourId);
}

Repository Class

public class GenericRepository<T> : IRepository<T> where T : class {
  private MyContext dataContext;
  private readonly IDbSet<T> dbset;  
  
  public GenericRepository(IDatabaseFactory databaseFactory)
  {
    DatabaseFactory = databaseFactory;
    dbset = DataContext.Set<T>();
  }

  protected IDatabaseFactory DatabaseFactory
  {
    get;
    private set;
  }

  protected MyContext DataContext
  {
    get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
  }
  
  // ... stuff removed for brevity ...
  
  public T GetById(Guid id)
  {
    return dbset.Find(id);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T19:48:02+00:00Added an answer on May 20, 2026 at 7:48 pm

    Try making protected virtual List _agents { get; set; } public

    public virtual List<Agent> _agents { get; set; }
    

    You can also eager load by doing something like this:

    _databaseContext.Tours.Include(x => x.Agents).Single(x => x.TourId == tourId) 
    

    you can read more here: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity named 'account' which will have a username column and I
I have created a Entity named MediaItem which is Abstract and Game inherits form
I have an entity in EF named Profile and I would like to add
I have a core data entity named Folder. Each Folder has a 1-to-many relationship
I have a table named ' entity ' in DB which consists of id,
Using CoreData, I have an entity Bookmark, that has an to-many relationship named 'tags'
When I have a CoreData entity named, say, 'Book', which has a one-to-one relationship
I have an entity named Commercial. I have an Category entity where the list
Lets say thay I have an entity with name A.It has two columns named
I have an old database (with terribly named tables and columns) and an entity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.