Updating collections using code first in Entity Framework 5 with detached
entities
I am developing an n-tier application with detached entities (Visual
Studio 2010). I have not included the class definitions as they seem
irrelevant to the logic.
The following code snippet works correctly and is embedded in a using
dbContext.
dbContext.Entry(Case).State = Case.CaseID == 0 ? EntityState.Added :
EntityState.Modified;
dbContext.Entry(Case.Woman).State = Case.Woman.CaseID == 0 ?
EntityState.Added : EntityState.Modified;
dbContext.Entry(Case.Summary).State = Case.Summary.CaseID == 0 ?
EntityState.Added : EntityState.Modified;
dbContext.SaveChanges();
I have added a collection ICollection<Cause> Causes to the Summary class.
What I want to do is:
Check to see if a new Cause is the same as the most recently saved Cause,
and, if so, change the value of a flag in the saved Cause
Insert the new Cause into the dbContext I can do this using two separate
contexts but would welcome a more code-first based way of doing this.
No comments:
Post a Comment