Association behaviour

8 posts, 1 answers
  1. Tajes
    Tajes avatar
    81 posts
    Member since:
    Sep 2009

    Posted 28 Jan 2011 Link to this post

    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
  2. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 28 Jan 2011 Link to this post

    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
  3. DevCraft banner
  4. Tajes
    Tajes avatar
    81 posts
    Member since:
    Sep 2009

    Posted 31 Jan 2011 Link to this post

    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.
  5. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 31 Jan 2011 Link to this post

    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

  6. Tajes
    Tajes avatar
    81 posts
    Member since:
    Sep 2009

    Posted 01 Feb 2011 Link to this post

    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.
  7. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 01 Feb 2011 Link to this post

    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
  8. Tajes
    Tajes avatar
    81 posts
    Member since:
    Sep 2009

    Posted 01 Feb 2011 Link to this post

    OK Thank you so much Henrik. The last post was very helpfull.
  9. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 01 Feb 2011 Link to this post

    Hi Ivan,

    Glad it worked out for you.

    /Henrik
Back to Top
DevCraft banner