This topic is focused on inserting entities in the database. It will demonstrate how to:
Performing Simple Insert Operations
In order to add new objects to the database, basically you need to perform the following steps:
- Create a new instance of the domain class.
- Initialize its properties.
- Pass it to the OpenAccessContext.Add method and invoke the SaveChanges method.
In the following example a new Car object is created. It is initialized with some default data. And finally it is passed to the OpenAccessContext.Add method.
| C# |
Copy Code |
|
using ( EntitiesModel dbContext = new EntitiesModel() ) { Car newCar = new Car(); newCar.Make = "Audi"; newCar.Model = "A8";
dbContext.Add( newCar ); dbContext.SaveChanges(); } |
| VB.NET |
Copy Code |
|
Using dbContext As New EntitiesModel() Dim newCar As New Car() newCar.Make = "Audi" newCar.Model = "A8"
dbContext.Add(newCar) dbContext.SaveChanges() End Using |
Setting One-to-Many Relations Between Objects
This subsection provides three examples demonstrating how to set one-to-many relations between objects. In the first example, new car and category objects are created. On the second step, the category object is assigned to the newCar.Category property. The newCar and newCategory objects are sent to the OpenAccessContext.Add method. Finally, SaveChanges is called. 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. Note that it is recommended to add the new objects to the context first, and then set the relation through the collection property.
| 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( newCategory ); dbContext.Add( newCar );
newCar.Category = newCategory;
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(newCategory) dbContext.Add(newCar)
newCar.Category = newCategory
dbContext.SaveChanges() End Using |
The second example is absolutely the same. The only difference is that only the newCar object is passed to the context. Note that newCategory object is not passed to the context. The point here is that you can create an entire hierarchy (tree) of objects and just pass the root object from the hierarchy to the OpenAccessContext.Add method. Adding the car object to the context implicitly adds all the objects referenced by car to the context as well. This powerful concept is called persistence by reachability. When invoking the SaveChanges method, Telerik OpenAccess ORM will add automatically records in the corresponding tables. The result in the database will be the same.
| 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 );
newCar.Category = newCategory;
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"
newCar.Category = newCategory
dbContext.Add(newCar)
dbContext.SaveChanges() End Using |
Using the Collection Property
The last example demonstrates 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. For example:
| C# |
Copy Code |
|
newCategory.Cars.Add(newCar); |
| VB.NET |
Copy Code |
|
newCategory.Cars.Add(newCar) |
However, this code will not work out of the box. You need to set the IsManaged property for the Cars collection to True. For more information about this scenario, check out How to: Manage Collection Properties.
Setting Many-to-Many Relations
Setting many-to-many relations is identical. Suppose, you have to a domain model including two entities (Product and Order) 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. 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() ) { 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. More information could be found in the How to: Manage Collection Properties.