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

How to add to the collection side of an artificial association

2 Answers 46 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Daniel
Top achievements
Rank 1
Daniel asked on 16 Apr 2013, 06:43 PM
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 Answers, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 1
answered on 16 Apr 2013, 08:25 PM
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.
0
Accepted
Yordan
Telerik team
answered on 19 Apr 2013, 03:43 PM
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");
        fluentContext.Add(product);
        product.SetFieldValue("ProductName", "My Product");
        product.SetFieldValue("Price", 24);
 
        object category = fluentContext.CreateInstance("FluentModel.Category");
        fluentContext.Add(category);
        category.SetFieldValue("CategoryName", "MyCategory");
        category.SetFieldValue("Description", "New Description");             
         
        IList products = category.FieldValue<IList>("Products");
        products.Add(product);               
 
        fluentContext.SaveChanges();
    }
}

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.

Regards,
Yordan
the Telerik team
Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Tags
Development (API, general questions)
Asked by
Daniel
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 1
Yordan
Telerik team
Share this question
or