Data Access has been discontinued. Please refer to this page for more information.

Consuming a Model - CRUD

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

To update data using Telerik Data Access, 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 Telerik Data Access 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 Telerik Data Access 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:

using System.Linq;
using OpenAccessModel;

namespace ConsumerProject
{
   class Program
   {
       static void Main(string[] args)
       {
           using (EntitiesModel dbContext = new EntitiesModel())
           {
               // Add a new Category.
               Category newCategory = new Category();
               newCategory.CategoryName = "New Category";
               dbContext.Add(newCategory);

               // Get the first Category using LINQ and modify it.
               Category firstCategory = dbContext.Categories.FirstOrDefault();
               firstCategory.CategoryName = firstCategory.CategoryName + "_Updated";

               // Commit changes to the database.
               dbContext.SaveChanges();

               // Use LINQ to retrieve Category with name 'New Category'.
               Category categoryToDelete = (from c in dbContext.Categories
                                            where c.CategoryName == "New Category"
                                            select c).FirstOrDefault();

               // Delete the 'New Category'from the database.
               dbContext.Delete(categoryToDelete);

               // Commit changes to the database.
               dbContext.SaveChanges();
           }
       }
   }
}
Imports OpenAccessModel

Module Module1
    Sub Main()
        Using dbContext As New EntitiesModel()
            ' Add a new Category.
            Dim newCategory As New Category()
            newCategory.CategoryName = "New Category"
            dbContext.Add(newCategory)

            ' Get the first Category using LINQ and modify it.
            Dim firstCategory As Category = dbContext.Categories.FirstOrDefault()
            firstCategory.CategoryName = firstCategory.CategoryName & "_Updated"

            ' Commit changes to the database.
            dbContext.SaveChanges()

            ' Use LINQ to retrieve Category with name 'New Category'.
            Dim categoryToDelete As Category = (
                From c In dbContext.Categories
                Where c.CategoryName = "New Category"
                Select c).FirstOrDefault()

            ' Delete the 'New Category'from the database.
            dbContext.Delete(categoryToDelete)

            ' Commit changes to the database.
            dbContext.SaveChanges()
        End Using
    End Sub
End Module

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.

Next Steps

For further reference, check out the CRUD Operations section.