How to add to the collection side of an artificial association

Thread is closed for posting
3 posts, 1 answers
  1. Daniel
    Daniel avatar
    4 posts
    Member since:
    Mar 2013

    Posted 16 Apr 2013 Link to this post

    I am having trouble finding samples of how to add objects to a collection of an artificial association.

    Say I have two artificial types, Header and Detail.
    Header has id property and a collection of Details.
    Detail has an id property and a Header property.

    The Header property of the detail can be set by something like:
    detailObject.SetFieldValue("Header", headerObject);

    But on the Header object side of the relationship, I can't find anything for working with collections.
    I would think I could get a reference to the collection as some kind of list and then add to it, but I can't find anything.

    Any help is appreciated.
  2. Daniel
    Daniel avatar
    4 posts
    Member since:
    Mar 2013

    Posted 16 Apr 2013 Link to this post

    silly me, this is easy.  

    casting the property descriptor's GetValue as an IList is all you have to do.  Don't know why I had such a hard time with this.
  3. Answer
    Yordan avatar
    39 posts

    Posted 19 Apr 2013 Link to this post

    Hi Daniel,

    You are right. You have to use SetFieldValue for setting ordinary properties and properties that are relations. Use FieldValue method to obtain reference to ordinary properties and properties that are collections. For example consider the following code snippet:

    static void Main(string[] args)
        using (FluentModel fluentContext = new FluentModel())
            object product = fluentContext.CreateInstance("FluentModel.Product");
            product.SetFieldValue("ProductName", "My Product");
            product.SetFieldValue("Price", 24);
            object category = fluentContext.CreateInstance("FluentModel.Category");
            category.SetFieldValue("CategoryName", "MyCategory");
            category.SetFieldValue("Description", "New Description");             
            IList products = category.FieldValue<IList>("Products");

    The important line here is IList products = category.FieldValue<IList>("Products");
    In this way we can obtain reference to the product collection of a given category and then we can add/remove products from there. More information about how to use artificial relations can be found in this article.

    If you have any more questions about artificial types and relations get back to us for assistance.

    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Back to Top