Data Manipulations with Entity Framework

When learning a new ORM one of the most important and interesting thing is to get to know how you can manipulate data with that tool.Thus, this particular thing is explained in this tutorial.

There are two ways of accessing data with the Entity Framework:

  • Entity SQL
  • LINQ to SQL

We will be using the latter in this tutorial. But first you need to create a project and generate a model. You can actually create any project you like, however I use the Console Project type in the examples. Second, you need to generate a model, so you go to Project –> Add Component and choose ADO.NET Entity Data Model

The next operations are quite obvious, you choose the database, select the tables (Employees, Territories and EmployeeTerritories). In the end you should get a graphical representation of the data model. As I have already written you will get just two entities instead of three. Anyway, I suggest the you rename some properties and entities.

Retrieving Data

That’s how you can retrieve the data:

NorthwindEntities objectContext = new NorthwindEntities();
var result = from e in objectContext.Employees
where e.EmployeeID == 6
select e;

Employee employee = result.First();
employee.Territories.Load();
Console.WriteLine(employee.LastName);

foreach (Territory territory in employee.Territories)
{
   Console.WriteLine(territory.TerritoryDescription);
}

If you have some experience with LINQ to SQL, then you won’t find anything new in this code, except for one thing, I’ll explain below. Basically, you create an instance of object context and perform a query. However, there is one difference between Entity Framework and LINQ to SQL, when working with Entity Framework, you have to explicitly load the data from from the linked tables. Pay attention to line 8, where such an operation is performed. At the same time, you can include related table in a LINQ query:

var result = from e in objectContext.Employees.Include("Territories")
               where e.EmployeeID == 6
               select e;

Please, note that you specify the name of the entities set in the Include() method, don’t confuse it with the name of the corresponding table.

Adding Data

Adding new data isn’t difficult as well. You simply create a new object and then add it and save the changes.

NorthwindEntities objectContext = new NorthwindEntities();
 
Employee employee = new Employee { FirstName = "John", LastName = "Doe" };
objectContext.AddToEmployees(employee);
 
objectContext.SaveChanges();

Modifying and Removing Data However, if you want to modify or remove an object, the object must be attached to the data context, this means that you either retrieve it from a storage or explicitly attach it to the object context by calling the AttachTo() method:

NorthwindEntities objectContext = new NorthwindEntities();
 
Employee employee = new Employee { EmployeeID = 1};
objectContext.AttachTo("Employees", employee);
employee.FirstName = "John";
 
Employee employeeToDelete = new Employee { EmployeeID = 10 };
objectContext.AttachTo("Employees", employeeToDelete);
objectContext.DeleteObject(employeeToDelete);

objectContext.SaveChanges();

In the following tutorials I’ll explain some fundamentals of the Entity Framework that one should be aware of when developing with the Entity Framework.

Mike Borozdin (Twitter)
1 October 2008

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way. My personal thoughts tend to change, hence the articles in this blog might not provide an accurate reflection of my present standpoint.

© Mike Borozdin