Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Microsoft Entity Framework in theory

Posted on: 2011-09-06

Microsoft Entity Framework (EF) is available with the Microsoft .Net Framework 3.5 since the first service pack. This is pretty interesting for WPF (Windows Presentation Foundation) form, Console or traditional Windows form application that does not need to add an additional assembly into their setup package.

What is Entity Framework in short?

In short, the Entity Framework is an ORM. An ORM is an Object Relational Mapping. The ORM goal is to map Business Logic (also know as Model classes) to the persistent storage. It lets the developer concentrate on model objects without caring about how to load/save them from the database. But, Entity Framework also can generate model objects from an existing database or on the other side can generate the database tables. This is very powerful and time saver.

How does Entity Framework works?

Entity Framework work under the hood with XML files. In fact, Visual Studio will hide you all the complexity of those XML files.

Communication

Entity Framework communicate to the database using multiple communication channel. This give some flexibility to the developer about how to access the persisted data. One way is directly to the EntityClient Provider using Entity Sql Query. While it's very powerful, the two others way are simpler and are mostly used. The second one is by using Entity SQL Query. This is like SQL statement but for the Entity Framework. It's also called ESQL. The third and last way to communicate is by using the Linq channel. Linq to Entities is very similar to Linq to Sql or Linq to Object but with some restrictions. So, the last two, HSQL and LinqToEntities pass through an additional layer called Object Service. This Object Service will be instanced in your code and queries will be done with it. The Object Service role is to translate the easy syntax into a Command Tree to be able to execute query via the ADO.NET Data provider. As you may already know, the Entity Framework is using ADO.NET technologies. So we went through the call from the code to the database. But, what happen once the database has executed the query? The data is sent back to the Object Service inside an EntityDataReader and the developer will receive all rows inside a IEnumerable. The T is the type of the entity requested to the Object Service.