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.
In your
Savemethod you set the state of the newPersontoEntityState.Added. When you do that, EF sets the state of each object in the object graph toAdded. It does not matter that theSessions 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.