This video shows how to utilize your OpenAccess ORM Domain Model for your Create, Retrieve, Update and Delete operations. (Runtime: 9:00)
To update data using Telerik OpenAccess ORM, you only need to modify the state of an object, you don't need to know any of the SQL performed on the backend. If your database happens to be any of the supported databases, the programming model does not change. The basic CRUD operations from a .NET and OpenAccess ORM perspective are:
- Create - instantiate a .NET object and add it to the OpenAccessContext by using the Add method.
- Read - use LINQ to retrieve objects from the database.
- Update - once you have a reference to a persistent object, you simply make changes to the object state.
- Delete - pass the object to be deleted to the OpenAccessContext by using the Delete method.
The next example demonstrates each of the CRUD operations using the SofiaCarRental database:
- Follow the steps in the Database First Scenario topic and create a new OpenAccess Class Library project. When you configure the database connection, use the SofiaCarRental database. Include all tables in the domain model.
- For the sake of simplicity, add a new Console Application to your solution. Follow the steps in the Consuming a Model - Configuration topic to ensure that the client project (in this example, this is the ConsumerProject) can use your domain model.
Here is the complete code:
static void Main(string args)
using (EntitiesModel dbContext = new EntitiesModel())
// Add a new Category.
Category newCategory = new Category();
newCategory.CategoryName = "New Category";
// Get the first Category using LINQ and modify it.
Category firstCategory = dbContext.Categories.FirstOrDefault();
firstCategory.CategoryName = firstCategory.CategoryName + "_Updated";
// Commit changes to the database.
// Use LINQ to retrieve Category with name 'New Category'.
Category categoryToDelete = (from c in dbContext.Categories
where c.CategoryName == "New Category"
// Delete the 'New Category'from the database.
// Commit changes to the database.
Using dbContext As New EntitiesModel()
Dim newCategory As New Category()
newCategory.CategoryName = "New Category"
Dim firstCategory As Category = dbContext.Categories.FirstOrDefault()
firstCategory.CategoryName = firstCategory.CategoryName & "_Updated"
Dim categoryToDelete As Category = (
From c In dbContext.Categories
Where c.CategoryName = "New Category"
What Just Happened?
Before starting CRUD operations against the domain model, you need to create a new instance of the OpenAccessContext. It is recommended to create a new instance of the context in the using statement. Thus, you will make sure that the context is properly disposed.
In order to add new objects to the database, basically you need to perform the following steps:
- Create a new instance of the object.
- Initialize its properties.
- Pass it to the OpenAccessContext's Add method.
The second operation shows you how to update an existing database object. You need a reference to the existing object from the database. Once you have it, just update its properties. In order to write changes to the database you need to call the SaveChanges method of the OpenAccessContext instance.
Then, a LINQ query is created and executed to retrieve the newly added category object from the database and delete it. In order to delete an object from the database the following steps should be performed:
- Retrieve a reference to the object.
- Pass the object to the OpenAccessContext's Delete method.
Finally, the well-known SaveChanges method is executed.
For further reference, check out the CRUD Operations section.