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 genera te 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, so that it looks like this:



Retrieving Data

That’s how you can retrieve the data:

   1: NorthwindEntities objectContext = new NorthwindEntities();
   3: var result = from e in objectContext.Employees
   4:              where e.EmployeeID == 6
   5:              select e;
   7: Employee employee = result.First();
   8: employee.Territories.Load();
   9: Console.WriteLine(employee.LastName);
  10: foreach (Territory territory in employee.Territories)
  11: {
  12:     Console.WriteLine(territory.TerritoryDescription);
  13: }

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.

   1: NorthwindEntities objectContext = new NorthwindEntities();
   3: Employee employee = new Employee { FirstName = "John", LastName = "Doe" };
   4: objectContext.AddToEmployees(employee);
   6: 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:

   1: NorthwindEntities objectContext = new NorthwindEntities();
   3: Employee employee = new Employee { EmployeeID = 1};
   4: objectContext.AttachTo("Employees", employee);
   5: employee.FirstName = "John";
   7: Employee employeeToDelete = new Employee { EmployeeID = 10 };
   8: objectContext.AttachTo("Employees", employeeToDelete);
   9: objectContext.DeleteObject(employeeToDelete);
  11: 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.