Cover Story: Get Ready for the Entity Framework

The industrial-strength Microsoft ORM is finally ready for prime time. Are you?

The ADO.NET Entity Framework 4, updated and released alongside the Microsoft .NET Framework 4 in April, is emerging as the default way to do data access in .NET-based applications. With the second generation of Entity Framework and related tooling in Visual Studio 2010, the Microsoft data access technology is better positioned for enterprise-level projects with a host of new functionality that brings it more in line with other object-relational mapping (ORM) tools.

The protests have died down with version 2 and the question of whether to use the implementation of LINQ known as LINQ to SQL -- the lightweight ORM that Microsoft shipped in 2007 -- or the Entity Framework, has been put to rest. LINQ to SQL is still an option but Microsoft has indicated that the Entity Framework now supports much of the functionality offered in LINQ to SQL and the company isn't putting any more resources behind it.

The Entity Framework is becoming a core part of the Microsoft technology stack. It works with WCF Data Services, WCF RIA Services, ASP.NET Dynamic Data, ASP.NET MVC and Silverlight, which does not support ADO.NET data sets.

"You can use LINQ to SQL or use your own classes as long as you follow certain rules, but a lot of those technologies are depending on a model. Entity Framework provides that model and Microsoft has built them so that they will very easily use an Entity Data Model from Entity Framework," says Julie Lerman, an independent .NET consultant who specializes in data platforms.

The Entity Data Model (EDM) provides a uniform way for developers to work with data by specifying the data structure of a client application through business logic, namely entities and relationships.

EDM consists of a conceptual data model (domain), model-based storage structures and a mapping specification that connects the two. It exposes ADO.NET entities as objects in .NET, providing an object-layer for Microsoft LINQ. Using LINQ to Entities, Entity SQL or query builder methods, developers can write queries against the conceptual model and return objects.

"It does remove all of the data access code that we used to have to write," says Lerman, who finished the second edition of her book, "Programming Entity Framework" (O'Reilly, 2010) in August. The book was rewritten to cover the Entity Framework 4 and Visual Studio 2010.

The Entity Framework takes a different approach than a lot of typical ORMs because the model has a mapping layer, which allows developers to really customize the model, Lerman says. "It's not just a direct representation of the database and that's a really important distinction."

"The goal of the Entity Framework is to be an abstraction layer on top of your database, so in theory it's supposed to reduce complexity," says Steve Forte, chief strategy officer at Telerik, which offers a competing ORM tool called OpenAccess. "However, developers know how to build applications the traditional way -- data over forms -- so the Entity Framework is something new that needs to be learned and the jury is still out on whether the learning curve is worth it."

Enterprise-Level Framework
The Entity Framework 1, which shipped out of band in Visual Studio 2008 SP 1 and the .NET Framework 3.5 SP 1 after dropping out of the beta cycle, was only braved by early adopters. It was deemed unusable for enterprise projects by many developers based on feature limitations and lack of adequate support for n-tier architectures.

"Until the most recent version of the Entity Framework, you could not easily build n-tier applications with it," says Lenni Lobel, chief technology officer at Sleek Technologies Inc. and principal consultant at twenty-six New York. Lobel says he's stayed away from the technology for enterprise-level production, but thinks the Entity Framework 4 is now viable for n-tier apps.

Many of the advances in the Entity Framework 4 were driven by feedback from developers who already used ORM tooling, according to Lerman. It now supports n-tier architectures within the framework via API and T4 templates, such as self-tracking entities. Microsoft also added key functionality, such as support for foreign key associations, lazy or deferred loading, and Plain Old CLR Objects (POCOs). Go to for more on the Entity Framework and POCOs.

"To me, the most important enhancement is the ability to support self-tracking entities once they're disconnected from the object context, but there's still a lot of work to be done," says Lobel, who notes that the POCO template is now a separate download after the beta cycle.

The Entity Framework 4 and Visual Studio 2010 support Model-First development, offering a Generate Database Wizard to create the database and parts of the EDM (SSDL, MSL) from a conceptual model. An Update Model Wizard helps you update the EDM when the database changes.

The EDM Designer has had a lot of improvements, such as how it handles complex types, but Microsoft couldn't come up with a way for people to deal with large models outside of a "large canvas," a complaint from the Entity Framework 1. "If your model is so large -- lots of entities -- that it's problematic in the designer, then you should probably be breaking the model up anyway, rather than asking the designer to enable you to work with it," says Lerman.

The EDM Wizard in Visual Studio 2010, which is used to create a conceptual model from an existing database (reverse engineering) and provide the database connections to your app, now supports foreign key associations and pluralization, which is important if you want to follow any kind of naming conventions for your entities.

"Previously, you had do lots of modifications to the model afterward," Lerman explains.

The Microsoft Edge
A few months after its release, the Entity Framework 4 seems to be holding up in comparisons with longstanding open source ORM NHibernate.

Mark Shiffer is a software architect for consultancy Capitol Strategies. He's working on a pilot project using the Entity Framework 4 for the data access layer of an IT operations system for a benefits administration agency that handles the Illinois state retirement system.

"We looked at NHibernate and Entity Framework; it was a toss up between the two. The shifting factor was Microsoft's backing of Entity Framework and it appears that they're going to stick with it," says Shiffer.

The project team, which consists of two consultants and four state employees, is rewriting the back-end IT operations. The design phase started a little less than a year ago, but the actual coding didn't start until February, when the Entity Framework 4 and Visual Studio 2010 were still in beta. The pilot project focuses on a human resources system.

The architecture for the project uses a Model-View-ViewModel design pattern with Windows Presentation Foundation, Windows Communication Foundation, Unity (an IoC container) and the Entity Framework 4 with Self-Tracking Entities. It also requires a repository pattern, and a Unit of Work pattern to provide transactional boundaries for data processes, according to Shiffer.

The IT operations system is a complete redesign, so the team is starting from scratch and using the new Model-First options in Visual Studio 2010 to create the conceptual model, and then generate the rest of the EDM and the database schemas. Shiffer is among the developers who ran into a problem with the number of entities in his model and limitations in the EDM designer.

Even so, he contends that the flexibility and level of customization supported in the Entity Framework 4 is critical for enterprise-level systems. "We had to write some custom workflow processes for the database generation piece and that's all pretty easy to figure out because it just runs off of some workflows and we use the Self-Tracking Entities template, too ... It was pretty easy to accomplish that, so that level of customization control was nice to see."

Shiffer says it's a matter of understanding the pieces of the Entity Framework, because the resources to plug in and extend the Entity Framework are there. "The Object State Manager and the Object Context [entity container] are key concepts to the framework and once you understand those concepts, it makes implementing some of the more intense features that we have to do for enterprise software easier," he says.

"We're still in the middle of digging around in there and implementing some of this stuff," he continues, "but the Repository pattern was very easy to implement within Entity Framework. Unit of Work was fairly easy as well, with the Object Context the way it's set up. But we're still having some issues with threading because the Object Context isn't thread-safe, so that's sending us for a bit of a loop right now."

Performance issues are the greatest concern in the pilot project, namely errant SQL generation, which has the project team trying to figure out whether the Entity Framework 4 is going to be efficient enough in the end. Shiffer describes a simple join and select for one record out of five or six tables, which ended up yielding up to 5,000 lines of SQL code for the select statement under the Entity Framework SQL.

"That's the biggest question mark right now for us, because it can spit out some pretty nasty SQL at times," says Shiffer.

Microsoft responded to Shiffer after he discussed some of these issues in his blog. "They're working on it and working to get it better," he says. Microsoft declined to comment for this article.

Will Shiffer and his team switch frameworks? That depends, he says, on what his team can do to optimize the SQL code coming out of the Entity Framework.

"There are various ways we could do that," Shiffer explains. "We could stick with Entity Framework and write stored procedure processes and have them called from there, but that takes away some of the benefits of using Entity Framework for an ORM. So that decision hasn't been made yet, that's why we're in the middle of a pilot project trying to test everything out."

Work in Progress
The Entity Framework 1 only supported Database First (reverse engineering). The Entity Framework 4 added a somewhat limited Model-First option. The Database Generation Power Pack available for download in the Visual Studio Code Gallery gives you more control over how the database schema is created by that domain model, according to Lerman.

"I'm a huge fan of the Model-First [option]," says Dane Morgridge, a Microsoft MVP for Data Platforms and solution architect at LiquidHub Inc. Morgridge has several projects using the Entity Framework 1 in production. "Previously, I was doing the database design in SQL Server Management Studio and ever since I started working with Visual Studio 2010, I've been doing all of my modeling within Visual Studio as opposed to SQL Server. And for what I've been doing, it has been working great."

Morgridge's team is using the Database Generation Power Pack -- which he says has huge potential -- to figure out templates, but he points out that the Power Pack forces developers to modify the T4 templates to create their own.

Lerman says that Microsoft got a lot of feedback from the ALT.NET community about things such as lack of POCO support. "POCO support in Entity Framework 4 opens up the possibilities for architecting with repositories, if that's what you want to do, and doing unit testing and really building applications where you can have a good separation of concerns, which is really important when you're architecting large applications," she says.

Morgridge says ORMs have made things easier for developers but they don't necessarily follow accepted patterns, such as repository patterns, out of the box.

"My recommendation to anybody starting out is to look at blogs that talk about how to use repository patterns and things like that, so that you can have clean separation," he says. "Unfortunately, a lot of sample code just talks about dropping an EDMX file right in the same folder as your project, and while that works, that's really not the right way to do things. So, Entity Framework really is what it is, it's just a matter of how you use it and what you do with it."

Morgridge's T4 Template for Repository and Unit of Work patterns in the Entity Framework is available on CodePlex.

Reality of Stored Procedures
How the Entity Framework works with stored procedures was another bone of contention in the Entity Framework 1, specifically in the area of data security requirements. Database administrators had a problem with letting the Entity Framework access database tables directly and do all of the command generation (SELECT, UPDATE, INSERT and DELETE), instead of using pre-defined stored procedures.

"If those stored procedures use output parameters, you're out of luck," says Sleek Technologies CTO Lobel. He says Microsoft needs to support stored procedures, "not just stored procedures with caveats."

Microsoft has done a lot to improve in that area, according to Lerman. "They worked really hard on how the queries are compiled, which essentially is the process by which you go from a LINQ to Entities query to a stored query, whether that's a T-SQL query in SQL Server or something in another database," she says.

Even in the Entity Framework 1, you could build models just from using stored procedures, according to Lerman, by building views on top of tables and then building a model around the views and stored procedures.

Just Programming
In late July, Scott Guthrie, corporate vice president of the .NET Developer Platform at Microsoft, got domain developers' attention with a series of blog posts about the upcoming Code-First option, a longstanding feature of NHibernate, OpenAccess and other ORM frameworks. Microsoft has talked about the Code-First feature since the Entity Framework 1, but has yet to deliver it.

"Depending on what you're doing, Code First is going to be as easy as LINQ to SQL," says Lerman. "It eliminates writing the visual model, you just write classes and then configure them a little further if you need to, to make them work for the Entity Framework."

She cautions that Code-First is still in the preview stage -- it's part of the Entity Framework Feature CTP 4 -- and not available for go-live production. But if Guthrie is blogging about it, chances are Microsoft has the features fairly nailed down.

Microsoft is responding to a "decently sized" segment of the market that would stay away from the Entity Framework without the Code-First design pattern, observes Forte.

"Microsoft's target audience with both LINQ to SQL and the Entity Framework is probably not the developer that goes Code First," he says, "but I think that the market dragged them in that direction, so it's actually pretty interesting and there's a good level of innovation that's coming out of it."

Other API enhancements in the Feature pack should make programming against the Entity Framework in the next release simpler. "In the core API, they're kind of wrapping some of the other functionality so that it's easier to find and easier to write," says Lerman.

When the simpler API was first announced in June, Rowan Miller, a Microsoft program manager on the Entity Framework Team, blogged:

At the heart of the Entity Framework Productivity Improvements are two new types, DbContext and DbSet<TEntity>. DbContext is a simplified alternative to ObjectContext and is the primary object for interacting with a database using a specific model. DbSet<TEntity> is a simplified alternative to ObjectSet<TEntity>and is used to perform CRUD operations against a specific type from the model. These new types can be used regardless of whether you created your model using the Entity Designer or code.

Hard Sell
The effort to reduce the complexity of the Entity Framework underscores Microsoft's task of pleasing enterprise-level ORM developers, many with a Java background, and others in the .NET community, who still need to be sold on the ORM concept.

Andrew Brust thinks Entity Framework should be an option, rather than the default way to do data access in .NET-based apps. "First of all, I don't love ORMs," says the Redmond Review columnist and chief of new technology at twenty-six New York. "I think they add an extra layer, which although elegant and helpful for large apps, is often overkill for smaller ones."

Many developers who use the Entity Framework aren't using the entities that it produces directly, but instead create their own wrapper classes, according to Brust. "As long as they're doing that then why not wrap ADO.NET objects directly?" he asks.

Morgridge says there are lots of advantages to using an ORM and while the complexity is there, he doesn't think it's great enough to warrant not using it. "It's really never overkill. The only place I wouldn't use an ORM is in a case where performance was an utmost factor because the whole additional layer can add some performance issues potentially," he says. "Sometimes you're not going to get the most performant-generated SQL out of LINQ expressions. If it's a case where performance is really an issue, then that's when I will go back and do a stored procedure or some other ADO.NET method."

Even with small projects, Morgridge says he saves a lot of time by using an ORM. "It cuts my data access code time by 70 percent easy, because I don't have to write hand-coded SQL statements and things like that."

"It's definitely worth taking a look at, especially if you're trying to compare using Entity Framework to using ADO.NET directly," says Shiffer. "It provides that wrapper that you need to separate your data layer out, and to abstract your concerns about SQL statements and not having to really be concerned with the data structure as much as you normally are if you didn't have an object-relational mapper in the middle of it all."

Riding the Coattails
Telerik's Steve Forte expects the growing popularity of other Microsoft technologies -- WCF RIA Services and OData with WCF Data Services -- to help drive Entity Framework adoption, and says he's already fielding questions along those lines from customers.

Brust holds a similar view. "Silverlight, WCF Data Services (aka "Astoria") and ASP.NET MVC are proving very popular; Entity Framework gets to ride coattails."

Hybrid applications are another form of adoption. "If you have a brownfield app and you've invested heavily in data over forms and stored procedures, that doesn't mean that there isn't room for an ORM like Entity Framework, or OpenAccess or NHibernate in your project," says Forte. "They have the ability to hook into reporting engines and hook into OData, and all three now hook into RIA Services. That's one thing that should be obvious, but it may not be."

It even works with the cloud. The Visual Studio 2010 design tools didn't initially support SQL Azure, so you couldn't point to a SQL Azure table and create a model from it, but that's changed. SQL Azure also lacked support for Multiple Active Result Sets (MARS), which would have affected Entity Framework 4 features like lazy loading, but Microsoft's database in the cloud has since added MARS support.

comments powered by Disqus


Subscribe on YouTube