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

Consuming a Model - CRUD

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 persistent class and add the object 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 FluentModelClient console application created with the Code Only Scenario tutorial.

Here is the complete code:

The code of the method that creates the database on the server is available in the How to: Create/Migrate Your Database article. A general recommendation is to add this method in the implementation of your context class.

using System.Linq;
using SampleFluentModel;

namespace FluentModelClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (FluentModel dbContext = new FluentModel())
            {
                // The method, which creates the database on the server.
                UpdateDatabase();

                // Add a new customer.
                Customer newCustomer = new Customer
                {
                    ID = 1,
                    Name = "John Smith",
                    EmailAddress = "john@smith.com",
                    DateCreated = DateTime.Now
                };
                dbContext.Add(newCustomer);

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

                // Get the first customer using LINQ and modify it.
                var customer = dbContext.Customers.FirstOrDefault();

                customer.EmailAddress = "john.smith@domain.com";

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

                // Use LINQ to retrieve a customer named John.
                var CustomertoDelete = dbContext.Customers.
                    Where(c => c.Name.StartsWith("John")).
                    FirstOrDefault();

                // Delete the customer from the database.
                dbContext.Delete(CustomertoDelete);

                // Commit changes to the database.
                dbContext.SaveChanges();
            }
        }
    }
}
Option Infer On
Imports SampleFluentModel

Module Module1
    Sub Main()
        Using dbContext As New FluentModel()
            ' The method, which creates the database on the server.
            UpdateDatabase()

            ' Add a new customer.
            Dim newCustomer As Customer = New Customer _
                With
                {
                    .ID = 1,
                    .Name = "John Smith",
                    .EmailAddress = "john@smith.com",
                    .DateCreated = Date.Now
                }

            dbContext.Add(newCustomer)

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

            ' Get the first customer using LINQ and modify it.
            Dim customer = dbContext.Customers.FirstOrDefault()

            customer.EmailAddress = "john.smith@domain.com"

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

            ' Use LINQ to retrieve a customer named John.
            Dim CustomertoDelete = dbContext.Customers.
                Where(Function(c) c.Name.StartsWith("John")).
                FirstOrDefault()

            ' Delete the customer from the database.
            dbContext.Delete(CustomertoDelete)

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

What Just Happened?

Before starting any CRUD operation against the 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 from the database a customer object with a specific value for the Name property. Next, this customer is marked for deletion from the database. 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.