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 9016631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:02:47+00:00 2026-06-16T04:02:47+00:00

Having an issue with trying to add a new entity that has a graph

  • 0

Having an issue with trying to add a new entity that has a graph of objects/entities that are not new.

Here’s my Domain types with Code First approach:

public class Person : IEntity
{
   public int Id { get; set; }
   public string Name { get; set; }
   public List<PersonSession> Sessions { get; set; }
}

 public class PersonSession : IEntity
 {
    public int Id { get; set; }
    public Person Person { get; set; }
    public Session Session { get; set; }
 }

public class Session : IEntity
{
  public int Id { get; set; }
  public string Title { get; set; }     
}

So I have a person, they can have many sessions, and there will be a table of their sessions. The Session table though will not have any reference back to persons, that’s what the PersonSessions table/relationship is for.

EF 5 does create these 3 tables using one DbSet.

But I am trying to make a simple Repository pattern with Generics and I have a Save method.

Only issue is it’s adding a new session to the Sessions table even when I use an existing session given to me by the Repository.GetAll();

 public int Save(T t)
 {
    if (t.Id == 0)
       _context.Entry(t).State = EntityState.Added;
    else
       _context.Entry(t).State = EntityState.Modified;

    _context.SaveChanges();
    return t.Id;
 }

So when I create 2 new sessions, then add them through the Generic Repo instance for Sessions, they get added correctly,

when I try and create a new person and give him those existing sessions like so:

  private static void AddPerson()
  {

     var newPerson = new Person
                        {
                           JobTitle = EJobTitle.SoftwareDeveloper,
                           Name = "Mark W",
                        };

     var sessionsForPerson = new List<PersonSession>();

     var session1 = _sessionRepo.Retrieve(1);
     var session2 = _sessionRepo.Retrieve(2);

     sessionsForPerson.Add(new PersonSession
                              {
                                 Session = session1,
                                 Person = newPerson
                              });

     sessionsForPerson.Add(new PersonSession
                              {
                                 Session = session2,
                                 Person = newPerson,
                              });

     newPerson.Sessions = sessionsForPerson;

     _personRepo.Save(newPerson);
  }

It’s adding the sessions a second time.

Is there a data attribute I can put over the PersonSession.Session property to tell it this will always be an existing session, or do I need to use something in the FluentApi to instruct EF?

thanks.

  • 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-06-16T04:02:48+00:00Added an answer on June 16, 2026 at 4:02 am

    In your Save method you set the state of the new Person to EntityState.Added. When you do that, EF sets the state of each object in the object graph to Added. It does not matter that the Sessions already have an Id > 0. This is unless the objects in the graph were previously retrieved by the same context. In that case they are and remain unchanged (and only the association will be saved).

    So that’s the problem. Your repositories each have a context of their own. It’s a common problem with generic repositories. One possible remedy is to create a context outside the repositories and inject it into the repositories that are involved in one business transaction. Another one is to make sure that existing objects enter a context in an unmodified state by traversing the object graph after adding a root entity.

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

Sidebar

Related Questions

I'm new to Obj-C/Cocoa programming, and I'm having an issue trying to dynamically add
today I'm having the issue that I'm trying to add a signature to a
I'm having an issue when trying to programmatically update a DateTime field that already
I am having an issue with a String array and trying to add it
i am having an issue trying to add images to a word document after
I am having an issue trying to make a small text-fantasy type game involving
I am having an issue trying to deal with timezones in the JQuery date/time
I am having an issue with trying to retrieve the contents of the view
I'm having an issue when trying to do something which should be as easy
I am having a big issue trying to delete from an UITableView. Just for

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.