How to: Insert Objects

This topic is focused on adding new objects to the database. Basically, you need to perform the following steps:

  1. Create a new instance of the domain class.
  2. Initialize its properties.
  3. 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#

using (EntitiesModel dbContext = new EntitiesModel())
{
Car newCar = new Car();
newCar.Make = "Audi";
newCar.Model = "A8";
dbContext.Add(newCar);
dbContext.SaveChanges();
}

VB

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

The next examples demonstrate 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 Data Access. Note that it is recommended to add the new objects to the context first, and then set the relation through the navigation property.

C#

 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

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 Data Access will add automatically records in the corresponding tables. The result in the database will be the same.

C#

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

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

See Also

Additional information about inserting objects through the navigation properties according to the type of the association is available in the Manage Navigation Properties section.