This is a migrated thread and some comments may be shown as answers.

Association behaviour

7 Answers 92 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tajes
Top achievements
Rank 1
Tajes asked on 28 Jan 2011, 10:54 AM
Hello,
   I've a question about the associations behaiour created in the visual designer. I have two entities with an association, both with navigation property implementing the association. When I set the navigation property of one of them with a new instance of the other, is set the navigation property in the new instance also?

class Entity1
{
      int id;
      Entity2 navProp;
}
 
class Entity2
{
       int id;
       Entity1 navProp;
}
 -------------------------------------------------
Entity1 entity1 = new Entity1();
entity1.navProp = new Entity2();
 
Entity2.navProp == ???? //is entity1 set?

thanks in advance and sorry for my English

7 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 28 Jan 2011, 03:14 PM
Hello Ivan,

I would say it depends on the association in question. The one you have in your example is a pure 1:1 (navigable in both directions) and in this specific case you have to set it on both ends of the association, as far as I know.

But, however, when doing 1:m (say you have a list of OrderLine instances (m) on an Order instance(1)), you can mark the collection as managed. By doing so, OpenAccess will handle both ends of the association. This is also true for m:n associations I believe.

Regards

Henrik
0
Tajes
Top achievements
Rank 1
answered on 31 Jan 2011, 09:19 AM
Thank you Henrik,
I tested it and it works but, the relation is only update when I add the objects to the database context, and don't before. I want ORM Open Access manage the relation between the entity objects even I don't attach them to database.
0
IT-Als
Top achievements
Rank 1
answered on 31 Jan 2011, 03:09 PM
Hello Ivan,

Glad it worked out.

I don't exactly know what you mean by:
"I want ORM Open Access manage the relation between the entity objects even I don't attach them to database"

Could you elaborate further?

Regards

Henrik

0
Tajes
Top achievements
Rank 1
answered on 01 Feb 2011, 10:23 AM
Hello Henrik,
I want to say that, I have marked the associatons as managed, and it works when the related objects are added to database but not before.

Example:
class Entity1
{
      int id;
 
      IList<Entity2> navProp; //-->navigation collection
}
  
class Entity2
{
       int id;
       Entity1 navProp; //-->navigation property
}
 -------------------------------------------------
Entity1 entity1 = new Entity1();
Entity2 entity2 = new Entity2();

entity1.navProp.add(entity2);
  
entity2.navProp == null  //---> this is true
 
DBcontext.add(entity1);
DBcontext.saveChanges();
 
Entity2.navProp == null  //---> now  is false

I want the associations were managed even without add the entities to database.

Thanks.
0
Accepted
IT-Als
Top achievements
Rank 1
answered on 01 Feb 2011, 10:31 AM
Hi Ivan,

Ok, now I get it..

Did you try to add your instance to the context immediately after the creation of it. Tracking of the object (in respect to (change)management and the like) is fully operational when the object is added to the context. Remember, adding the object to the context is NOT the same as adding it to the database. First on commit(SaveChanges) the changes registered in the context will be flushed (saved) to the database.

In general I always find it a nice pattern to add the instances to the context, right after creation, since OA will then manage the object for me.

Something like:

class Entity1
{
      int id;
 
      IList<Entity2> navProp;
}
  
class Entity2
{
       int id;
       Entity1 navProp;
}
 -------------------------------------------------
Entity1 entity1 = new Entity1();
DBcontext.add(entity1); // Now OA manages the object
entity1.navProp.add(new Entity2());
  
Entity2.navProp == null  //---> this is true
 

DBcontext.saveChanges();
 
Entity2.navProp == null  //---> now  is false

Try it... and let me know if this solves the problem...

Regards

Henrik
0
Tajes
Top achievements
Rank 1
answered on 01 Feb 2011, 01:42 PM
OK Thank you so much Henrik. The last post was very helpfull.
0
IT-Als
Top achievements
Rank 1
answered on 01 Feb 2011, 03:01 PM
Hi Ivan,

Glad it worked out for you.

/Henrik
Tags
Getting Started
Asked by
Tajes
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Tajes
Top achievements
Rank 1
Share this question
or