When Entity Framework (EF) 3.5 was first released in 2008, I cried out ‘What the heck’, just like many other developers did across the world. As misunderstood as this technology was, some found it buggy while others felt it was complex. There was also a ‘natural’ resistance from the developer community, especially the ones working on LINQ To SQL a.k.a. Linq2Sql. They simply could not find a good reason for using EF 3.5 over LINQ to SQL.
However with the Entity Framework 4.0 release this year, developers have changed their opinion. In this article, I will introduce Entity Framework 4.0 to DotNetCurry readers, the history behind this framework and why it has become the recommended LINQ-based Data Access technology over LINQ To SQL, all in a simple Q & A format.
Note: I will be starting this new series of articles on Entity Framework (EF 4.0). So stay tuned if you want to learn EF 4.0! One way is to Subscribe to the RSS Feed and stay updated.
What is Entity Framework 4.0? Why do we need a new Data Access Technology?
All these years, Microsoft has been providing different ways to communicate with the Database. Developers have been switching from one data access technology to the other, starting with DAO (Data Access Objects), then RDO (Remote Data Objects), then ADO (ActiveX Data Objects) and then finally ADO.NET. ADO.NET has been around from quite some time and Microsoft has invested a great deal of resources in it, with new features added to it every now and then. Although working with the DataSet and DataReader has served our data access needs for many years, a developer spends a lot of time being concerned with the details of the schema, trying to keep up with database schema changes and doing redundant tasks while interacting with the database, over and over again.
The ADO.NET Entity Framework (EF) is an Object/Relational mapping (ORM) framework and is a set of technologies in ADO.NET, for developing applications that interacts with data. We just discussed that an ADO.NET developer spends a lot of time keeping up with the database changes. Well EF provides a mapping from the relational database schema to the objects and offers an abstraction of ADO.NET. So with EF, you can define Entity classes that are independent of a database structure and then map them to the tables and associations of the database. Since we are now working with Entities which have their own schema, we are shielded from the changes in the database. The object context keeps tabs on the entities that are changed.
In simple words, with the Entity Framework, you are architecting, designing and developing at a conceptual level. You are no more worried about the ‘specific details’ of communicating with the database and switching from one relational database to the other is also possible with EF, without much efforts.
What happens to ADO.NET now?
ADO.NET is here to stay! EF was not released with the intension to replace ADO.NET. It is in fact an enhancement to ADO.NET and helps eliminate the gap between the application and the database. Behind the scene, EF uses ADO.NET classes, but the details are abstracted from you. EF provides a shift from Database-oriented (DataReader, DataSet) to Model-oriented development. So instead of focusing on a Database, a developer focuses on the Entity that represents the Business Model of the application.
If the purpose of EF is not yet clear, it will be, in the forthcoming articles when we will explore its features and look at it in detail. Continue reading as I am about to explain why EF was chosen over LINQ To SQL!
When was Entity Framework Released?
Although announced in TechEd 2006, the first version of Entity Framework 3.5 was released two years later in August 2008, along with Visual Studio 2008 SP1 and .NET 3.5 SP1.
The second version (also the current version) of Entity Framework i.e. 4.0 was released in April 2010 along with VS 2010 and .NET 4.0.
What happened to LINQ To SQL?
When .NET 3.5 and Visual Studio 2008 was being released in November 2007, a lot of focus was on LINQ. The original plan was to release EF along with VS 2008/.NET 3.5 and introduce developers to this new LINQ based data access technology. However the scale of EF caused a delay and it could not be released along with VS 2008.
Meanwhile, LINQ To SQL (L2S), worked upon by a different team at Microsoft was ready to release. Since one of the focuses of the VS 2008 and .NET 3.5 release was on LINQ, it made a lot of sense to release a LINQ Provider along with VS 2008. So during that time, L2S became the original LINQ provider for data access. In my honest opinion, L2S was kind of a bait to attract developers towards LINQ data access, while the team continued working on EF, i.e. the real beast.
Finally when EF 3.5 was released in August 2008, it could not get the attention from the developer community, for obvious reasons. L2S was gaining popularity primarily due to it being lightweight, simple and easy to use technology and the fact that most of the .NET apps targeted SQL Server, there was a resistance to adopt a new framework (EF) from the dev community. EF remained a misunderstood technology, there were issues in the framework and although accidental, L2S was still believed by many as the original LINQ provider for data access. EF had a lot to prove!
However with the release of EF 4.0 with .NET 4.0, things have changed, including opinions. Microsoft made it clear that although L2S was supported, it was not the recommended technology. Moreover with the new features like POCO support, Model First Development and Custom Code Generation, developers started looking at EF 4.0 and consider it seriously as the preferred ORM solution for enterprise apps.
LINQ To SQL Vs Entity Framework
There are some fundamental differences between L2S and EF which every developer should understand. Both EF and L2S provide a framework for managing relational data as objects, with the objective of programming against a conceptual model rather than a DB schema.
However LINQ To SQL supports rapid development of applications that query only SQL Server and SQL Server Compact 3.5 databases, providing a 1:1 mapping of your existing Database schema to classes. It does not provide the flexibility to use objects that do not exactly match the tables.
EF on the other hand, supports advanced modeling features and ‘loosely coupled and flexible’ mapping of objects to SQL Server. Through extended ADO.NET Data Providers, EF supports other relational databases as well, including Oracle, DB2, MySql etc. EF also allows objects to have a different structure from your database schema.
Although LINQ To SQL (L2S) is supported by Microsoft, it is not recommended. Entity Framework (EF) is definitely the way to go if you are working on something bigger (enterprise apps), need the flexibility of a solid framework, with the support for multiple databases and much more!
Note: LINQ to Entities is part of the Entity Framework and exposes many of the same features as L2S. It has replaced L2S as the standard mechanism for using LINQ on databases.
How do I get Started with Entity Framework 4.0?
Well if you have continued reading so far, I assume you are interested in Entity Framework 4.0 and are ready to explore the future of data access. The easiest way to get started with it is to download and install Visual Studio 2010.
The Entity Framework 4.0 is part of .NET 4.0, so both Visual Studio 2010 and the .NET 4.0 provide you with the necessary tools and technologies to begin developing. So you can either download the Visual Studio 2010 Ultimate Trial Edition or get the free Visual Studio 2010 Express Edition.
Update: Check the next articles in this series
Create an Entity Data Model From a Database – Entity Framework 4.0
Exploring how the Entity Data Model (EDM) Generates Code and Executes Queries – Entity Framework 4.0
Model-First Development in Entity Framework 4.0 - Create a Database from a Model
Create an Entity Framework Model and Use it in Multiple Projects
Add, Update and Delete Objects in Entity Framework 4.0
Bind Entity Framework 4 Model to ASP.NET GridView
Profiling and Logging Entity Framework Queries
I hope you liked the article and I thank you for viewing it!
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!