Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
How to: Manage Collection Properties
Programmer's Guide > Developer's Guide > CRUD Operations > How to: Manage Collection Properties

Glossary Item Box

This topic discusses how to manage collection properties in the context of one-to-many and many-to-many associations. Generally, there are two ways to set relations between objects. The first one and most simple is to set the navigation properties (e.g. car.Category) to the right entities. OpenAccess ORM will make sure to populate the foreign keys with the right values. This topic is focused on the second approach. It will show you how to use the collection properties (e.g. category.Cars).

There is one important thing to remember. If you are adding an item to an IList<T> association, you should make sure to set the IsManaged property of the association to True in the Visual Designer. This will instruct OpenAccess to track additions and removals of items in the collection and issue the proper statements during SaveChanges(). By default, the IsManaged property is set to False for all your associations for performance reasons. Setting IsManaged to True could lead to decreased performance when working with large databases, containing hundreds of thousands of objects participating in such relationships. That's why the default value for IsManaged is False.

 

One-to-Many Associations

Suppose, you have the following domain model (it is based on the SofiaCarRental database). The model includes two entities related with one-to-many association. The Car entity has one instance of Category. You can use the Car.Category property to access the Category instance that is associated with an instance of the Car entity. The Category entity has many instances of Car. You can use the Category.Cars property to access the Car instances that are associated with an instance of the Category entity.

As it is demonstrated in the How to: Insert Objects topic, the most simple way to set a relation between Car and Category objects is to use the car.Category property. In this case two object are added to the database. First, a new record is added to the Categories table, and second, a new record is added to the Cars table. Also the Car.CategoryId foreign key column is automatically initialized by Telerik OpenAccess ORM.

C# Copy Code
newCar.Category = newCategory;
VB.NET Copy Code
newCar.Category = newCategory

There is another approach you can use to specify the relation between the Car and Category objects. Instead of using the newCar.Category property, you can use the newCategory.Cars collection property.

C# Copy Code
newCategory.Cars.Add(newCar);
VB.NET Copy Code
newCategory.Cars.Add(newCar)

However, there is one important condition here. You need to set the IsManaged property of the Category.Cars collection in the Visual Designer to True.

What will happen if you don't set the IsManaged property for category.Cars to True? In the best case, you will have two new records in the database, respectively in the Car and Category tables. If the car.CategoryId foreign column allows null values, it will be set to null; otherwise, you will get an exception.

Here is a complete code snippet showing you how to set a new relation by using the collection property. Note that it is recommended to add the new objects to the context first, and then set the relation through the collection property. The final step is to invoke the SaveChanges method.

C# Copy Code
using ( EntitiesModel dbContext = new EntitiesModel() )
{
   Car newCar =
new Car();
   newCar.Make =
"Audi";
   newCar.Model =
"A8";

   Category newCategory =
new Category();
   newCategory.CategoryName =
"MyCategory";

   dbContext.Add( newCar );
   dbContext.Add( newCategory );

   newCategory.Cars.Add( newCar );

   dbContext.SaveChanges();
}
VB.NET Copy Code
Using dbContext As New EntitiesModel()
    Dim newCar As New Car()
    newCar.Make = "Audi"
    newCar.Model = "A8"

    Dim newCategory As New Category()
    newCategory.CategoryName = "MyCategory"

    dbContext.Add(newCar)
    dbContext.Add(newCategory)

    newCategory.Cars.Add(newCar)

    dbContext.SaveChanges()
End Using

 

Removing Relation Between Objects

To remove an existing relation between objects, simply use the collection.Remove method.

C# Copy Code
category.Cars.Remove(car);
VB.NET Copy Code
category.Cars.Remove(car)

This code will remove the relation between the category and car objects, i.e. it will set the car.CategoryId foreign key column in the database to null. Again, to get this code working you need to set the IsManaged property of the collection in the Visual Designer to True.

 

Many-to-Many Associations

Setting many-to-many relations is identical. Suppose, you have to a domain model including two entities with many-to-many relation between them.

The following code-snippet demonstrates how to create new Order and Product objects and initialize a new relation between them. Again, it is recommended to add the new objects to the context first, and then set the relation through the collection property. The final step is to invoke the SaveChanges method.

C# Copy Code
using ( EntitiesModel dbContext = new EntitiesModel() )
{
   Product newProduct =
new Product();
   newProduct.Name =
"TestProduct";

   Order newOrder =
new Order();
   newOrder.OrderDate = DateTime.Now;

   dbContext.Add( newProduct );
   dbContext.Add( newOrder );

   newOrder.Products.Add( newProduct );

   dbContext.SaveChanges();
}
VB.NET Copy Code
Using dbContext As New EntitiesModel()
    Dim newProduct As New Product()
    newProduct.Name = "TestProduct"

    Dim newOrder As New Order()
    newOrder.OrderDate = Date.Now

    dbContext.Add(newProduct)
    dbContext.Add(newOrder)

    newOrder.Products.Add(newProduct)

    dbContext.SaveChanges()
End Using

Again, there is one important step you need to perform here. You have to set the IsManaged property of the Order.Products collection in the Visual Designer to True.

Respectively, if you want to use the reverse collection (i.e. newProduct.Orders), you have to set the IsManaged property of the Product.Orders collection in the Visual Designer to True.