If you use LINQ to SQL, you may wonder why Microsoft released yet another ORM (Entity Framework), while they already had one. You may also be confused, because it’s not obvious which one to choose. Anyway, the Entity Framework has its own shiny features that don’t present in LINQ to SQL. Besides, you can still use LINQ to retrieve data with the Entity Framework, in this case, it’s called LINQ to Entities, at the same time you can also use a SQL like language – Entities SQL. In this story I’ll show one particular feature of Entity Framework that certainly must interest many developers.
y, Entity Framework provides a higher level of abstraction than LINQ to SQL. In fact, in hides the data level. So, you deal with objects that represent real entities, not database tables, like if you were using LINQ to SQL. I mean when you generate LINQ to SQL classes, it creates classes for each tables, even though those tables are merely junction tables without any reflection in our real life.
Let’s have a look at the example, to make things clear.
In the Northwind database we have tables – Employees and Territories that are linked with each other by using the third one – EmployeesTerritories.
Entity Framework modeler generates this schema:
There are a lot of interesting things here. First of all, there is no junction table, instead there are navigation properties that allows us to access the corresponding territories or employees.At the same time, since the Employees table also points to itself, there are the navigation properties (Subordinates and Manager that are responsible for that kind of relations. Second, there are no foreign keys, we have navigation properties instead of them as well.
Thus, the Entity Framework allows us to deal with objects that clearly represent real life entities, instead of objects that just reflect tables.
That’s just one of many sexy features of the Entity Framework. I’ll try to cover them in the next articles, moreover I’m going to put a comparison between LINQ to SQL and Entity Framework in terms of programming, because sometimes it’s not so easy to migrate from LINQ to SQL to Entity Framework because of some pitfalls.