dev guy

Adventures of a dev guy

Why use the Entity Framework?

Why use the Entity Framework?

Rate This
  • Comments 46

There are a number of places where you can read an introduction to the Entity Framework, listen to a podcast about it, or watch a screen cast or video of an interview.  Even with these various resources, though, there are so many different data access technologies out there that it's not uncommon for me to get the question: Why should I use the Entity Framework?  Or what differentiates it from other options like just using ADO.Net SqlClient and friends, LINQ to SQL or something like nHibernate?  I like the second question better, because the truth is that different problems merit different solutions.  So here's just a quick take on my perspective about these:

Entity Framework vs. traditional ADO.Net
All of the standard ORM arguments apply here.  The highlights are that you can write code against the Entity Framework and the system will automatically produce objects for you as well as track changes on those objects and simplify the process of updating the database.  The EF can therefore replace a large chunk of code you would otherwise have to write and maintain yourself.  Further, because the mapping between your objects and your database is specified declaratively instead of in code, if you need to change your database schema, you can minimize the impact on the code you have to modify in your applications--so the system provides a level of abstraction which helps isolate the app from the database.  Finally, the queries and other operations you write into your code are specified in a syntax that is not specific to any particular database vendor--in ado.net prior to the EF, ado.net provided a common syntax for creating connections, executing queries and processing results, but there was no common language for the queries themselves; ado.net just passed a string from your program down to the provider without manipulating that string at all, and if you wanted to move an app from Oracle to SQL Server, you would have to change a number of the queries.  With the EF, the queries are written in LINQ or Entity SQL and then translated at runtime by the providers to the particular back-end query syntax for that database.

Entity Framework vs. LINQ to SQL
The first big difference between the Entity Framework and LINQ to SQL is that the EF has a full provider model which means that as providers come online (and there are several in beta now and many which have committed to release within 3 months of the EF RTM), you will be able to use the EF against not only SQL Server and SQL CE but also Oracle, DB2, Informix, MySQL, Postgres, etc.

Next there is the fact that LINQ to SQL provides very limited mapping capabilities.  For the most part L2S classes must be one-to-one with the database (with the exception of one form of inheritance where there is a single table for all of the entity types in a hierarchy and a discriminator column which indicates which type a particular row represents).  In the case of the EF, there is a client-side view engine which can transform queries and updates made to the conceptual model into equivalent operations against the database.  The mapping system will produce those views for a variety of transformations.

You can apply a variety of inheritance strategies: Assume you have an inheritance model with animal, dog:animal & cat:animal.  You can not only do what L2S does and create a single table with all the properties from animal, dog & cat plus a column that indicates if a particular row is just a generic animal or a dog or a cat, but you can also have 3 tables where each table has all of the properties of that particular type (the dog table has not only dog-specific columns but also all the same columns as animal), or 3 tables such that the dog and cat tables have only the key plus those properties specific to their type of animal and retrieving a dog object would involve a join between the animal table and the dog table.  And you can further combine these strategies so some parts of a hierarchy might live in one table and some parts in separate tables.

In addition you can do what we call "entity splitting" where a single type has properties which are drawn from two separate tables, and you can model complex types where there is a type which is nested within a larger entity and which doesn't have its own separate identity--it just groups some properties together.  The best example of this is something like address where the street, city, state and zip properties go together logically, but they don't have independent identity.  The address is only interesting as a set of properties that are part of a customer or whatever.  As you have noticed, for v1 you can't create complex types with the designer in the EF--you have to code them by hand in the XML files.

Entity Framework vs. nHibernate
Because nHibernate is a rather full-featured ORM, the distinguishing features between the EF and it are not as large.  In fact, it is certainly true that nHibernate is a more mature product and in many ways has more ORM features than the EF.  The big difference between the EF and nHibernate is around the Entity Data Model (EDM) and the long-term vision for the data platform we are building around it.  The EF was specifically structured to separate the process of mapping queries/shaping results from building objects and tracking changes.  This makes it easier to create a conceptual model which is how you want to think about your data and then reuse that conceptual model for a number of other services besides just building objects.  Long-term we are working to build EDM awareness into a variety of other Microsoft products so that if you have an Entity Data Model, you should be able to automatically create REST-oriented web services over that model (ADO.Net Data Services aka Astoria), write reports against that model (Reporting Services), synchronize data between a server and an offline client store where the data is moved atomically as entities even if those entities draw from multiple database tables on the server, create workflows from entity-aware building blocks, etc. etc.  Not only does this increase the value of the data model by allowing it to be reused for many parts of your overall solution, but it also allows us to invest more heavily in common tools which will streamline the development process, make developer learning apply to more scenarios, etc.  So the differentiator is not that the EF supports more flexible mapping than nHibernate or something like that, it's that the EF is not just an ORM--it's the first step in a much larger vision of an entity-aware data platform.

  • PingBack from http://microsoft.wawblog.info/?p=31610

  • I've prepared dozens of articles and presentations on the Entity Framework over the past year and a half and the questions always start with "Why would I want to use this instead of XYZ?" LINQ to SQL, a variety of ORM tools, straight up ADO.NET, custom

  • I've prepared dozens of articles and presentations on the Entity Framework over the past year and

  • I've prepared dozens of articles and presentations on the Entity Framework over the past year and

  • Daniel Simmons vient de publier un post sur lequel il explique pourquoi EF c'est bien en le comparant

  • Danny Simmons has a post on why to use Entity Framework .  Here's my main gripe with Danny's

  • But I don't want EDM. I don't want to model my RELATIONAL MODEL database in my OBJECT ORIENTED .NET system.

    I want to MAP from one incompatible model to another, which is why I use an OBJECT/RELATIONAL Mapper.

    From what you're describing, EF is not an O/RM at all, it's a data model mapper to allow you to bring all your relational data modeling problems into your object-oriented world, making the relational problems even more complicated than they were before.

    It turns out databases are pretty good at dealing with relational models and .NET is good at dealing with object models and the two are quite different. Wouldn't it be easier to take advantage of the strengths of both systems and push the ugliness of mapping from one system to another into a framework like NHibernate?

    From what I've seen, and from what you're saying w/r/t to EDM, EF is all about the classic data access approach of materializing my relational model as objects.

    It seems we've been trying this for many years in various incarnations and it's never been successful and I doubt this one will work well because the fundamental problem is: You can't work effectively with relational models in an object-oriented world (or vice versa).

  • Have you looked carefully at the mapping capabilities of the EF?  EDM is not a relational model.  It is a conceptual model which is much like the object-oriented system you get in .net, and as such it can do (and eventually will do) all that an ORM can. The limitation of an ORM, though, is that you can't easily reuse the mapping exercise you have done and the mental model you have created with your object model in other contexts.

    That said, I'm not trying to convince everyone to drop nHibernate, that the EF is better than nHibernate or any such thing.  I'm trying to help clarify what makes them different so that you can make your own decision about the best tool for your situation.

  • You've been kicked (a good thing) - Trackback from DotNetKicks.com

  • Danny Simmons wrote a marketing piece about the project he's been working on for so long: "

  • @dan

    I actually don't care that much about NHibernate. I care about layered architecture, separation of concerns and, specifically, persistence ignorance.

    All hats off to the NHibernate folks, but to be honest, NHibernate has a lot of warts and isn't the perfect O/RM, but it's the best we've got so far (from my experience).

    So if EF does a better job at SoC and PI, then I'm all for it. But (again, admittedly only from what I've HEARD/READ), it sounds like EF requires me to litter my domain model with all sorts of persistence concerns and necessarily forces me to think about persistence when working with the domain model and business logic. This is not good and is a step in the wrong direction and is only marginally better than SQL statements directly in the middle of my business logic (both involve persistence concerns where they shouldn't be).

    To be fair, I'm not JUST picking on EF here, because other so-called 'ORM's make this mistake also.

  • @chad

    Your concerns about persistence ignorance are understood.  Sadly with all of the other things going on in the first release we were unable to get all the way there.  There are things you can do to mitigate these concerns somewhat in v1, but I think you'll see a better picture in v2.

  • I think it's a mistake to share a data model with anyone outside your bounded context (see Evans, Domain-Driven Design).  It's also a mistake to share a conceptual model or a EDM or whatever we're calling this.

    I never want domain objects exposed directly via services.  That's violating the encapsulation I'm trying to create.

    If someone wants SSRS, then I give them a separate reporting database, tailored to reporting needs.  I don't want reporting concerns bleeding into my transactional concerns.  A mapping layer will not solve this problem, products like SSIS can.  You want reporting? Here's your read-only view, updated every hour, five minutes, daily, whatever.

    Sharing a connection string is sharing a connection string, no matter how much lipstick you put on that pig.

  • Dan Simmons from the Entity Framework team at Microsoft made a nice post comparing the Entity Framework

  • Dan Simmons from the Entity Framework team at Microsoft made a nice post comparing the Entity Framework

Page 1 of 4 (46 items) 1234
Leave a Comment
  • Please add 2 and 2 and type the answer here:
  • Post