views:

35860

answers:

11

Now that .NET v3.5 SP1 has been released (along with VS2008 SP1), we now have access to the .NET entity framework.

My question is this. When trying to decide between using the Entity Framework and LINQ to SQL as an ORM, what's the difference?

The way I understand it, the Entity Framework (when used with LINQ to Entities) is a 'big brother' to LINQ to SQL? If this is the case - what advantages does it have? What can it do that LINQ to SQL can't do on its own?

+29  A: 

There are a number of obvious differences outlined in that article @lars posted, but short answer is:

  • L2S is tightly coupled - object property to specific field of database or more correctly object mapping to a specific database schema
  • L2S will only work with SQL Server (as far as I know)
  • EF allows mapping a single class to multiple tables
  • EF will handle M-M relationships
  • EF will have ability to target any ADO.NET data provider

The original premise was L2S is for Rapid Development, and EF for more "enterprisey" n-tier applications, but that is selling L2S a little short.

KiwiBastard
+30  A: 

I think the quick and dirty answer is that

  • LINQ to SQL is the quick-and-easy way to do it. This means you will get going quicker, and deliver quicker if you are working on something smaller.
  • Entity Framework is the all-out, no-holds-barred way to do it. This means you will take more time up-front, develop slower, and have more flexibility if you are working on something larger.
Brad Tutterow
You'll also tend to write less lines of code with L2S to accomplish the same thing as you would with EF. No lazy loading in EF means you're always checking if something was loaded or not.
Paul Mendoza
Nice comment. I like answers that break things down into the basics and leave "philosophy and history" to white papers.
Phil
+3  A: 

If your database is straightforward and simple, LINQ to SQL will do. If you need logical/abstracted entities on top of your tables, then go for Entity Framework.

vintana
Entity Framework allows for a layer of abstraction of top of the database. The problem with many OR Mappers today (in my opinion) is that they provide a 1 to 1 mapping between tables and classes. The database model doesn't always reflect the way that we think about it in terms of a business model.
senfo
Ran out of space. Anyhow, based on what I said above, I'd argue that your answer isn't complete.
senfo
@vintana - I think this is really bad advice. L2S is good *regardless* of the simplicity or complexity of your database. The real trap is not having a proper separation of concerns. If you try to merge your business layer and your data access layer, and use your Linqed up objects for everything, then you'll find L2S limiting. But that's a problem with an overly simplistic and monolithic design. L2S makes a great DAL, and if you consider querying and persistence a separate concern from your business rules, you'll save yourself lots of trouble in lots of areas in the long run.
mattmc3
+83  A: 

LINQ to SQL only supports 1 to 1 mapping of database tables, views, sprocs and functions available in Microsoft SQL Server. It's a great API to use for quick data access construction to relatively well designed SQL Server databases. LINQ2SQL was first released with C# 3.0 and .Net Framework 3.5.

LINQ to Entities (ADO.Net Entity Framework) is an ORM (Object Relational Mapper) API which allows for a broad definition of object domain models and their relationships to many different ADO.Net data providers. As such, you can mix and match a number of different database vendors, application servers or protocols to design an aggregated mash-up of objects which are constructed from a variety of tables, sources, services, etc. ADO.Net Framework was released with the .Net Framework 3.5 SP1.

This is a good introductory article on MSDN: Introducing LINQ to Relational Data

Kris
+36  A: 

Is LINQ to SQL Truly Dead? by Jonathan Allen for InfoQ.com

Matt Warren describes [LINQ to SQL] as something that "was never even supposed to exist." Essentially, it was just supposed to be stand-in to help them develop LINQ until the real ORM was ready.

...

The scale of Entity Framework caused it to miss the .NET 3.5/Visual Studio 2008 deadline. It was completed in time for the unfortunately named ".NET 3.5 Service Pack 1", which was more like a major release than a service pack.

...

Developers do not like [ADO.NET Entity Framework] because of the complexity.

...

as of .NET 4.0, LINQ to Entities will be the recommended data access solution for LINQ to relational scenarios.

Zack Peterson
Actually, we don't like EF because it has such a poor designer and is so extremely, *extremely* buggy. I've never found it to be all that complex.
BlueRaja - Danny Pflughoeft
+7  A: 

My impression is that your database is pretty enourmous or very badly designed if Linq2Sql does not fit your needs. I have around 10 websites both larger and smaller all using Linq2Sql. I have looked and Entity framework many times but I cannot find a good reason for using it over Linq2Sql. That said I try to use my databases as model so I already have a 1 to 1 mapping between model and database.

At my current job we have a database with 200+ tables. An old database with lots of bad solutions so there I could see the benefit of Entity Framework over Linq2Sql but still I would prefer to redesign the database since the database is the engine of the application and if the database is badly designed and slow then my application will also be slow. Using Entity framework on such a database seems like a quickfix to disguise the bad model but it could never disguise the bad performance you get from such a database.

TT
Your missing the point -- even with small databases, you may want something different than a 1:1 relationship between database tables and code/domain objects. Just depends on how much abstraction you want in the bus/domain objects.
alchemical
I have realized that :) Today I like to handcode my business entities. I still use Linq2sql but only inside my repositories where I get data using Linq2sql and the convert the linq2sql entities into my custom business entities. Maybe a bit more work than using a or-mapper but still I like to keep my business layer free of any OR-mapper specific code.
TT
+17  A: 

My experience with Entity Framework has been less than stellar. First, you have to inherit from the EF base classes, so say good bye to POCOs. Your design will have to be around the EF. With LinqtoSQL I could use my existing business objects. Additionally, there is no lazy loading, you have to implement that yourself. There are some work arounds out there to use POCOs and lazy loading, but they exist IMHO because EF isn't ready yet. I plan to come back to it after 4.0

Jiyosub
Lack of POCO support is the number one reason I have been choosing LINQ to SQL over the Entity Framework. I may revisit EF when they incorporate it in the next version, as they are promising to. There are some additional projects out there that do POCOs for EF, but not cleanly enough.
joseph.ferris
+1 I agree it still needs a lot of work.
James
In case someone (like me) doesn't know what POCO stands for: [Plain Old CLR Object](http://en.wikipedia.org/wiki/Plain_Old_CLR_Object "Wikipedia - Plain Old CLR Object")
CBono
I really don't see what the big fuss about not supporting POCOs is...it's one more level of abstraction guys. Create a factory, injecting the data repository and construct your POCOs there. It's probably a good idea anyway.
Stimul8d
A: 

Hi everybody

I think if you need to develop something quick with no Strange things in the middle, and you need the facility to have entities representing your tables:

Linq2Sql can be a good allied, using it with LinQ unleashes a great developing timing.

Regards!

MRFerocius
+1  A: 

Neither yet supports the unique SQL 2008 datatypes. The difference from my perspective is that Entity still has a chance to construct a model around my geographic datatype in some future release, and Linq to SQL, being abandoned, never will.

Wonder what's up with nHibernate, or OpenAccess...

John Dunagan
A: 

i am getting the impression that Entity Framework is better practice to use and offers better functionality than LINQ2SQL

iceangel89
Until EF 4.0 comes out, EF is lacking and produces worse code than LINQ2SQL does. The lack of lazy loading is a massive burden when developing any EF application.
Paul Mendoza
+1  A: 

There's more players in this game than just L2S and L2EF. (i.e. Plinqo, LLBLGen)

A lot of programmers have put their names down panning L2EF and suggesting to all comers that L2S is the way to go. There's also guys and gals out there vigorously supporting L2EF. It's not clear cut.

L2S continues to be updated, so it's not dead.

This is not an easy decision and one size does not fit all.

I didn't realise what was going on and jumped into L2EF for a project that was intended to be quick. I soon went back to what I know, L2S, to keep project timing on track. (I don't write off using L2EF, but I don't expect to use it soon.)

After I wasted some time on this I put together a demo web application to help guide this decision. There's a short post about it at http://decisionz.wordpress.com/2010/04/09/suggestions-for-linq-tool-to-use/ that links to the web page. Might be worth a look.

Mike Gale