One-sided association and IsManaged

4 posts, 0 answers
  1. Petr
    Petr avatar
    9 posts
    Member since:
    Apr 2014

    Posted 21 Sep 2015 Link to this post

    Is there a way have managed one-sided association?  Let's take this model for example (adding IsManaged() at the end). I'd like to be able to issue

    var vendor = new Vendor { Name = "Vnd" };
    vendor.VendorContacts.Add( new VendorContact { ContactType = "Type" });
    model.Add(vendor);
    model.Save();
     

     Sadly when I try to run this I get null exception and I have to save the contact first

    var vendor = new Vendor { Name = "Vnd" };
    var contact = new VendorContact { ContactType = "Type" };
     
    model.Add(contact);
    model.Save();
     
    vendor.VendorContacts.Add( contact );
     
    model.Add(vendor);
    model.Save();

  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 24 Sep 2015 Link to this post

    Hello Petr,

    The purpose of setting the IsManaged flag of the association to true is to let Telerik Data Access to synchronize both ends of the association. The first benefit of this is that the Foreign Key column/property will be populated automatically. The second benefit is that the child object will be added in the context because it will be persisted when both ends of the association are synchronized. When one end of the association is missing, there will be no effect of managing the association, because the two ends of the association cannot be synchronized and the second object cannot be persisted. This is why you are receiving the null exception.

    I hope that helps.

    Regards,
    Boris Georgiev
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. Petr
    Petr avatar
    9 posts
    Member since:
    Apr 2014

    Posted 25 Sep 2015 in reply to Boris Georgiev Link to this post

    Hey Boris,

     so I can take that as

    vendor.VendorContacts.Add( new VendorContact {Name = "Name"} );
      
    model.Add(vendor);
    model.Save();

    cannot be achieved for a one sided associations. Would that be right?

    Thanks~

  5. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 25 Sep 2015 Link to this post

    Hello Petr,

    Yes, that is right. This behavior cannot be achieved for one sided association. You should have two sided association to get the benefits of using the IsManaged behavior.

    If any other questions arise, do not hesitate to contact us again.

    Regards,
    Boris Georgiev
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top