In-Depth

Entity Sequel

Microsoft's second release of ADO.NET Entity Framework 4 could bring object-relational mapping to more developers, but exactly how many remains to be seen.

When Microsoft released the first version of its ADO.NET Entity Framework (EF) in .NET 3.5 SP 1, the technology was widely derided by seasoned database developers for being a day late and a dollar short. It offered too little for everyday database developers.

Microsoft sucked up the criticism, promising its second take on the EF would address many of the features missing that are available in other object-relational mapping (ORM) tools, notably NHibernate. It appears Microsoft has answered a good portion of its critics' complaints.

On Oct. 19, the company released Visual Studio 2010 beta 2 and the .NET Framework 4 beta 2. Included was EF4 beta 2, representing the feature-complete view of Microsoft's second stab at offering an ORM tool.

The EF is Microsoft's ambitious attempt to provide object-relational mapping between .NET applications and n-tier databases, other repositories and ultimately the cloud. Developers use the Entity Data Model (EDM) as the specification for defining the entities and relationships used by an application built on the EF. EDM consists of a design schema to build programmable classes, model-based storage structures and a mapping spec that connects the two. It exposes ADO.NET entities as objects in .NET, providing an object-layer for the Microsoft Language Integrated Query (LINQ). Using LINQ to Entities, developers can write queries against a database from the same language used to develop the business logic.

Like other ORM tools, Microsoft embarked on the EF so that developers could more productively build next-generation applications by modeling their apps rather than programming directly against different data schema. It's intended to help developers build and change applications over time without regard to the underlying repository.

Though it's actually version 2, Microsoft is calling the new release EF4 to align it with .NET 4. EF version 1 supported only "schema-first" development. In EF4, developers will finally be able to leverage the new "model-first" capabilities with the updated EDM Wizard and EDM Designer in Visual Studio 2010.

Shaky Start
The EF had a rough start. First Microsoft missed its goal of including it with the initial release of Visual Studio 2008 and .NET 3.5 two years ago. Then, right before its release in SP1, a vocal group of critics largely from the ALT.NET community issued a widely publicized rebuke. Nearly 800 developers have signed the "vote of no confidence," an online petition pointing to the shortcomings of the initial EF. Among the criticisms was the framework's focus on the data aspect of entities and its failure to support Plain Old CLR Objects (POCO), lazy loading and persistence ignorance, the principle by which applications and domains are not dependent on one another.

The first release was a database design-first framework rather than Model First, says Roger Jennings, principal consultant, OakLeaf Systems and a VSM contributor. Jennings refers to the organizers of the petition as the "NHibernate Mafia."

An open source alternative, NHibernate has a robust ecosystem and enjoys broad support among enterprises. As such, Microsoft says it endorses the ALT.NET community's efforts. NHibernate is also appealing to shops that have both .NET and Java applications, and is a more mature ORM environment than the EF and LINQ, which both surfaced in .NET 3.5. Proponents say shops that standardize on Visual Studio and the .NET Framework will ultimately start using the EF as their means of modeling applications in the future.

In addition to having a feature-complete, model-first architecture and support for complex data types in the Entity Designer, EF4 will support persistence ignorance and the building of n-tier apps within the framework via API and T4 templates. The new beta adds support for foreign-key associations, improvements to POCO, lazy loading by default and code-only support, which provides the option of skipping the model altogether when needed.

"There's some heavy-hitting stuff," says Julie Lerman, an independent .NET consultant, who is in the process of updating her book "Programming Entity Framework" (O'Reilly and Partners, 2009). "It's changed the way I'm looking at how I'm structuring my applications. There are a lot of organizations that have already made the commitment that they are going to use the Entity Framework and they have either been struggling with the first version or waiting for this next version. I'm encouraging everyone I speak with to start looking at beta 2."

Many developers who have tested EF4 beta 2 in the weeks since its release agree it addresses many of the crucial shortcomings of prior releases, including the beta 1 released back in June. "I find the Entity Framework bits included with Visual Studio 2010 beta 2 to be a huge improvement" says Dane Morgridge, senior interactive developer at Roska Direct Advertising in Montgomeryville, Pa.

Evident in EF4 is the long-awaited support for what Microsoft calls model-first development. Developers using the ADO.NET Entity Model Designer can now start from a model and subsequently have T-SQL and generate custom code, according to Microsoft. Support for T4 Templates in Visual Studio 2010 integrated with the Entity Framework Designer will offer developer-controlled code generation.

In the first release, developers leveraging the EDM had only one option: to reverse engineer it from an existing database, says Jonathan Carter, a technical evangelist for the .NET Framework and Visual Studio at Microsoft, in a Channel 9 video (see the video here).

With the EF4 and model-first development, developers can create an EDM that includes entities and associations, while the tooling can generate the SQL to create a database.

"If that's the type of development you prefer and you don't want to have to think about the database," says Carter, "well, you don't have to. You can just leverage the Entity Data Model and the Entity Framework to do your data access."

ORM Tool of Choice?
Despite such promises, Morgridge says he was skeptical that the model-first development would be ready for prime time. But after taking beta 2 for a spin in late October, he was pleasantly surprised at the database he was able to generate. It produced a .SQL file that executes on the database, which then created the tables and relationships from the model, he explains in a blog posting (read the entire blog here).

"All in all I'm very impressed with where the Entity Framework is headed and despite its critics, I believe it to be an excellent choice for doing data access in .NET," he says. "Version 4 will definitely answer a lot of the issues that the critics are using against it. I have used many ORM tools in my development career and currently the Entity Framework is my ORM tool of choice."

Elisa Flasko, a program manager with the Microsoft Data & Modeling Group, says the EDM is pivotal in the company's effort to simplify how developers map their applications against n-tier repositories. "The Entity Data Model represents such a key strategic direction for Microsoft that spans all of our products," Flasko says. "The older languages and frameworks are important technologies we want to have to enable our customers and partners to increase their productivity and better enable the integration across their applications and across the many different data sources."

Indeed, the first version of the EF fell way short of achieving that, says Ben Dewey, a senior software developer at twentysix New York. Dewey attempted to use the first version to build an application for a recording studio to book sessions. He used the EF with an existing WinForms application going against a database. Because of EF's weak support for stored procedures in version 1, Dewey had to automatically inject parameters such as calling user rolls in order to update and modify columns.

"We found ourselves going around Entity Framework more than we were using it," Dewey recalls. "It ended up being replaced by a library that reflects on our objects to set stored procedure properties." As a result, Dewey and his colleagues decided to start over, scrapping the EF and instead returning to his tried-and-true use of traditional ADO.NET. "It really wasn't working for us," he says of the EF. "We ended up finding ourselves wrapping Entity Framework and the other stuff that was actually more code than we were replacing."

When beta 2 was released in October, Dewey decided to give it another try. The verdict? If he had what's in beta 2 when building the studio-booking app, it would have worked. "There wouldn't have been any argument," Dewey says. "It provides the ability to update the results sets from the stored procedures, which is exactly what we were looking for."

The improved support for stored procedures provides the ability to use the designer to map complex data types, Flasko explains. "We've made it easier to work with stored procedures to bring them in and choose what parts of the stored procedure you're actually implementing," she says. "In 3.5 SP1, you had to implement all of the parts of the stored procedure, or you needed to implement not only the read but also the update, create and delete as well. We've made a number of improvements around that in Entity Framework 4 in general."

Core to building n-tier applications using the EF4 is ADO.NET Data Services, Microsoft's framework consisting of patterns and libraries that allow developers to build and consume RESTful Web services. "When using a relational database, you can build directly on top of the Entity Framework," explains Flasko, "but there's a model as well with data services to build on top of other types of data sources."

With the first version of Entity Framework developers were able to build on top of custom objects, but with beta 2, one of the key improvements is the addition of the data service provider interface for custom provider writers. Developers can create a custom provider underneath data services, Flasko says.

The Path to Agile Development?
Lerman, who has worked closely with Flasko's team, has probably spent more time working with EF4 than most developers outside of Microsoft. A new interface in beta 2 allows developers to more easily unit test their applications. "For me it's a big exploration; I'm not by nature an Agile developer," says Lerman. "But having these capabilities in there will encourage more developers to do more Agile development and think about their architecture and think about separation of concerns."

So will the new EF win over critics in the ALT.NET community and those partial to NHibernate? "I'd say Entity Framework 4's feature list compares favorably with NHibernate," says Jennings. "Entity Framework 4 certainly is easier for those new to object-relational mapping to approach."

While it may be easier, it's a far cry from NHibernate, critics say. "Only in the latest version do they have anything resembling POCO support, but it's not as mature as the NHibernate equivalent," says Chad Myers, a .NET consultant who co-developed Fluent NHibernate. Instead of writing .HBM.XML files, FluentNHibernate lets developers write mapping in C#.

"For example, you don't have as fine-grained control over fetching strategies, which can result in Entity Framework loading too many entities from the database," Myers says. "The proxy support-at least in the beta-behaves strangely if you don't mark your properties as virtual. NHibernate fails fast and tells you what the problem is."

Myers also argues that the EF is still XML-centric. "NHibernate has one XML document-sometimes a fragment of an XML document-per entity. Entity Framework has three or four-depending on how you count it-XML-mapping documents per entity." Lerman says the new code-only option lets developers work around that. But Myers says the code-only mapping option lacks many key features that are in the Fluent NHibernate companion project for NHibernate.

Stephen Bohlen, organizer of the New York ALT.NET Group, says EF4 is slowly but steadily improving to address some of its shortcomings in comparison to other more mature ORM tools. "Entity Framework 4 remains somewhat constrained in its ability to improve by some of the initial design decisions that constituted version 1," Bohlen says.

"Following its historic practices, Microsoft is loathe to introduce breaking changes in subsequent versions of their software-for better or for worse," he continues. "Despite this, Entity Framework 4 does a reasonable job of attempting to address the concerns of others in the ORM community such as some of the version 1 shortfalls. Entity Framework 4 is beginning to evolve into a reasonable choice of an ORM tool for those constrained by the need to remain within the bounds of a commercial ORM tool, although even within the commercial ORM space, Entity Framework 4 continues to lag with regard to features and ease of use."

Lerman says much of that criticism may be valid, but for a vast majority of developers new to using ORM and modeling, many of those issues are going to be moot. "I think there's going to be enough in there for a lot of those developers, but there's going to be others who don't think it's enough," she says. "It's doing something the ALT.NET community was hoping Microsoft would do for a long time, which is to engage the typical developer and encourage them to start using more Agile practices and doing unit testing."

Winning over the LINQ to SQL Crowd
But there's another large community of developers Microsoft needs to win over: those who invested in the company's LINQ to SQL component of .NET 3.5, which provides lighter-weight object mapping to relational data than EF. While it appeared the two would co-exist as ORM alternatives, Microsoft last year said it will not make any major improvements to LINQ to SQL and said the EF will be its core ORM platform.

It was a move that culminated after a high-level decision to move all of the work being performed on LINQ providers (including LINQ to SQL) by those developing languages into the Data Platform group, according to sources familiar with the events.

The Data Platform group's reasoning was that EF using LINQ to Entities is designed to connect to multiple back-ends, including Microsoft SharePoint Server and Windows Azure cloud services, as well as third-party repositories including IBM DB2 and Oracle databases. Nevertheless, the decision drew backlash from numerous shops that had decided LINQ to SQL would serve their needs for applications connecting to SQL Server.

"A lot of people like LINQ to SQL, but the numbers are going to start declining when the full version of Entity Framework 4 comes out and simpler types of models are able to be achieved easier," says Jim Wooley, an independent .NET developer, MVP and co-author of the book "LINQ in Action" (Manning Publications, 2008). "For really small applications, LINQ to SQL still has some role and there still will be some enhancements, though mostly performance and bug tweaks."

For all its improvements, there are still some that believe the EF has a long way to go. "It appears that Microsoft has entered the ORM stage in a big way, but they usually flounder with their initial offerings," says Jason Short, CEO of VistaDB Software Inc., provider of an embedded .NET-based SQL database engine. "The .NET 4 version of Entity Framework seems to be much improved over the previous bolt-on release to 3.5 SP1," he says.

But Short questions why SQL is the underlying glue in ORM tools. "I don't understand why Microsoft is continuing the insistence that the EF uses SQL. Most database engines internally don't use SQL to get their data. So why should the ORM communicate only through SQL?"

Yet, for all its critics, it appears EF4 is poised to win over a growing number of developers. The only question is, how many? "Those that were not the early adopters may start to look at Entity Framework to a point, whereas they were hesitant to do it before," Wooley says. "It will have broader appeal, but it's not going to be seen as the panacea that some may want."

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube