Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects

I've read some questions/answers that have to do with this particular error message, but I'm not quite understanding the appropriate solution.

I've read many times that you should create the EF4 context, use it, then dispose of it. Throughout my application, I'm loading entities here and there using different context objects, and then eventually want to associate the entities together.

I've create a simple console application that easily causes the error. The very simple model is diagrammed followed by the code.

How can I get the two different entities to share the same context? Do I really have to create a new context, load the two entities again (even though I already have them), simply to associate them and save?

If I simply missed an already existing, appropriate question/answer, please point me to the right place.

Simple EF4 Model diagram

internal class Program {      private static void Main(string[] args) {         DeleteAllEntities();         CreateInitialEntities();         Owner o = LoadOwner();         Child c = LoadChild();         AssociateAndSave(o, c);     }      private static void AssociateAndSave(Owner o, Child c) {         using (var context = new ModelEntities()) {             // Exception occurs on the following line.             o.Children.Add(c);             context.Attach(o);             context.SaveChanges();         }     }      private static Owner LoadOwner() {         using (var context = new ModelEntities()) {             return ((from o in context.Owners                      select o).First());         }     }      private static Child LoadChild() {         using (var context = new ModelEntities()) {             return ((from c in context.Children                      select c).First());         }     }      private static void CreateInitialEntities() {         using (var context = new ModelEntities()) {             Owner owner = new Owner();             Child child = new Child();             context.Owners.AddObject(owner);             context.Children.AddObject(child);             context.SaveChanges();         }     }      private static void DeleteAllEntities() {         using (var context = new ModelEntities()) {             List<Child> children = (from c in context.Children                                     select c).ToList();             foreach (var c in children)                 context.Children.DeleteObject(c);             List<Owner> owners = (from o in context.Owners                                   select o).ToList();             foreach (var o in owners)                 context.Owners.DeleteObject(o);             context.SaveChanges();         }     } } 
like image 883
Steve Avatar asked Mar 07 '13 15:03

Steve


2 Answers

You should get a reference to the child and owner objects in the same context. An approach for this would be to get the ids and then pass them as parameters to the AssociateAndSave(int oId, int cId) method.

Then, you retrieve the references to the objects in the same context and you make the attachment.

private static void Main(string[] args) {     DeleteAllEntities();     CreateInitialEntities();     int oId = LoadOwnerId();     int cId = LoadChildId();     AssociateAndSave(oId, cId); }  private static int LoadOwnerId() {     using (var context = new ModelEntities())     {         return (from o in context.Owners                 select o).First().Id;     } }  private static int LoadChildId() {     using (var context = new ModelEntities())     {         return (from c in context.Children                 select c).First().Id;     } }  private static void AssociateAndSave(int oId, int cId) {     using (var context = new ModelEntities())     {         var owner = (from o in context.Owners                         select o).FirstOrDefault(o => o.ID == oId);         var child = (from o in context.Children                         select o).FirstOrDefault(c => c.ID == cId);          owner.Children.Add(child);         context.Attach(owner);         context.SaveChanges();     } } 
like image 85
Alex Filipovici Avatar answered Sep 20 '22 09:09

Alex Filipovici


You cannot delete objects from one context, with an instance of another context, because each context tracks its objects separately.

One ideea would be to provide each of your methods a parameter of your context type so your operations are made within the same context.

EX:

private static void CrudOperationExample(DBContext contextInstane) {     //perform crud operations. } 

I you want to do it more nicely, i suggest you look for dependency injection, as it is very much helpful in ORM's .

like image 31
Freeman Avatar answered Sep 24 '22 09:09

Freeman