Simple C-Sharp Samples using Entity Framework

Common C# Database Operations using Entity Framework

The following are common simple database operations using Entity Framework for dot net developers. These are meant as code snippets and examples of how to use Entity Framework with VistaDB, not a comprehensive tutorial on .Net database programming.

What does this demonstrate?

The following examples assume that you have created an ADO.NET Entity Data Model from a VistaDB database and added it to your project (we used the Northwind sample). To watch a video of creating an Entity Framwork model see our VistaDB 4 EntityFramework Quickstart video on YouTube. The Employee entity object used below reflects a table named Employee in the Northwind.vdb4 database that the ADO.NET Entity Data Model was created from.

Looking for Help Learning Entity Framework?

The following are some helpful resources for learning the ADO.Net Entity Framework. It is a large topic, and has the added complexity of having to learn LINQ at the same time. LINQ is not part of EF, but rather a core part of .Net. But the way you write queries against your database in EF is by using LINQ.

How to create a new object and insert it using an entity model

All Entity Framework objects have to be accessed through a Context object. You name the context when creating the model, in these examples we used a name of Entities.

Objects are allocated by calling new on the object, but that does not associate the object with the Data Context. You call the context.AddToEmployee() with the new object to add it to the context, then save the changes to commit them to disk.

public static void CreateNewEmployee(string firstName, string lastName)
{
    using (Entities model = new Entities())
    {
        Employee emp = new Employee();
        emp.FirstName = firstName;
        emp.LastName = lastName;
        model.AddToEmployee(emp);
        model.SaveChanges();
     }
}

How to read data from a database using an entity model

Load each employee from the entities collection and output their first and last name. This is a simple foreach over the data context Employee collection. This will by default load ALL employees. You can filter this list by applying a where clause to the model.Employee object. We didn't include that in the same in order to keep it simple.

public static void ReadEmployees()
{
    using (Entities model = new Entities())
    {
        Console.WriteLine("Employees.........");
        foreach (Employee emp in model.Employee)
        {
             Console.WriteLine(emp.EmployeeID + " " + emp.FirstName + " " + emp.LastName);
        }
    }
}

How to update a row in a table using an entity model

Objects in the Entity Framework are similar to any other .Net object. You make property changes to them by setting their properties and calling methods. But to save the changes back to the database you must call SaveChanges() on the data context.

public static void UpdateEmployee(string oldName, string newName)
{
    using (Entities model = new Entities())
    {
        foreach (Employee emp in model.Employee)
        {
            if (emp.FirstName == oldName)
            {
                emp.FirstName = newName;
             }
       }
       model.SaveChanges();
   }
}

Delete a row from a table using an entity model

Using the same Entity Context object you can also tell it to delete an object. Objects that are deleted are tracked as a change, so SaveChanges() still has to be called to commit the changes back to the database. In this code we actually go through each employee and delete it if the name matches the name passed into the function. You could actually filter the employee list using a .Where clause to do a match of the employee name first (and avoid loading them all), but we didn't want to complicate this sample with where logic.

public static void DeleteEmployee(string name)
{
    using (Entities model = new Entities())
    {
        foreach (Employee emp in model.Employee)
        {
            if (emp.FirstName == name)
            {
                model.DeleteObject(emp);
            }
         }
         model.SaveChanges();
    }
}