The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,868,426
since: 19 Jan 2005

LINQ to Entities vs. LINQ to SQL - What should I use and when?

posted Thu 17 Aug 06
The CTP of ADO.NET vNext just came out recently, which includes a build of the ADO.NET Entity Framework. While the EF includes things like Entity SQL and so on, the parts of the EF that I am mainly concerned with are the EDM (Entity Data Model) itself, the Mapping Provider for ADO.NET, and most importantly: LINQ to Entities.

LINQ to Entities allows me to write C# LINQ queries that run against a conceptual data model. This is huge. I cannot stress enough to you how unbelievably forward-thinking and useful it is to be able to run queries against a conceptual object model as opposed to a database schema. The issue that a lot of folks have is in trying to decide if they need to use the Entity Framework or if they are fine with plain-vanilla LINQ to SQL (formerly called DLINQ). The following table provides a summary of the feature set (there is admittedly much more than what I've included in this small table, but this at least made the decision easy for me to make) comparison between the two systems. What you need to realize is that LINQ to Entities is a superset of LINQ to SQL. If the entities you have created in your EDM are backed by a SQL Server 2005 database, LINQ to Entities (L2E for short from now on) will create dynamic SQL statements that connect the entity model with the live database instance and provide your code with results.

Feature

LINQ to SQL

LINQ to Entities

Language Extensions Support

Y

Y

Language Integrated Database Queries

Y

Y

Many-to-Many (3way Join/Payload relationship)

N

N

Many-to-Many (No payload)

N

Y

Stored Procedures

Y

N (to be added)

Entity Inheritance

N

Y

Single Entity From Multiple Tables

N

Y

Identity Management / CRUD features

Y

Y


So rather than discuss the areas in which L2SQL and L2E are similar, I'm only going to cover the points at which they diverge.

First is the object model. When you use SQLmetal and DLINQ to create an object model from a database schema, you are consuming the database schema and immediately creating classes that represent that schema. These classes are slathered with attributes that tell DLINQ how to communicate with the database. If you need to change the object model, you must do so directly within the generated classes. If you change the database schema, you either have to figure out how to manually tweak the generated classes, or re-run SQLmetal. The Entity Framework provides a layer of abstraction above the database : you define a conceptual object model, you define the database schema in XML, and then you create a logical mapping between your conceptual object model and the database schema. Using this model, you are extremely well insulated from database changes. Most database changes can be absorbed by the schema and mapping without requiring a change to the object model - making it so you don't need to refactor and rebuild a bunch of objects that you're using throughout your application.

The second major difference is only made possible by the first. The immense power of the abstraction layer above the database provided by the Entity Framework gives you the ability to do things like entity inheritance and entity composition. This means that you can create an entity that is composed of columns originating in multiple tables without writing any complex join logic.

Building on entity composition is entity inheritance. This allows you to create entities that inherit from each other. For example, you could create an entity called VideoGame in your online retail application. There are many different kinds of video games, but doing things like:

...... WHERE gm.GmType = 402 ....

Just isn't very object-oriented. It makes many programmers cringe to have to look at stuff like that (and if it doesn't make you cringe, shame on you!). What we really should be able to do is something like this:

var ps2Games = from game in AllGames where game is Playstation2Game orderby game.Rating select game;

Now that is code that I can read, I can refactor it, it is easy to maintain, and I don't need to go track down the original author of the code to figure out what the intent of the code is. All that, and it still accomplishes the same high-performance result as the original T-SQL query. Using entity inheritance and conditional properties you can create classes that respond to the OOP-familiar is keyword but still perform the same gm.GmType = 402 schema-dependent query in the backend database.

So, to sum it up, here's my advice:
  • If you want the added security of insulation and loose coupling from the underlying database schema to make your object model more resilient to change, use the Entity Framework
  • If you find that you need the features of entity inheritance and entity composition, use the Entity Framework
  • If you already have a large DLINQ codebase (oddly enough, I do) that is running just fine without entities, you probably don't need to spend the time to refactor out DLINQ to replace it with L2E.
  • If you want to run LINQ queries against an object model, but your object model is a 1:1 mirror of the tables in your database, you probably don't need the EF.
  • Not included in the chart above, ADO.NET vNext has a powerful "client-views" engine which will only get better with time and is just more incentive for adopting the new stuff.
In short, while ADO.NET vNext and the Entity Framework is certainly a hot new thing, there are a few cases where you might be better off spending your time elsewhere. For the rest of you, stay tuned because I'm going to put up some Entity Framework samples in the coming days.

p.s. Thanks to Pablo from the ADO.NET team for clarifying that LINQ to Entities does not actually use what we used to refer to as DLINQ for generating dynamic SQL statements for Entity Model mapping to database schema, that is handled by a shared engine as he mentions in a comment on this post.

tags:                        

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Damodar Periwal left...
Thu 17 Aug 06 4:03 pm :: http://www.softwaretree.com

Nice first cut. A couple of questions.

"you define a conceptual object model, you define the database schema in XML, and then you create a logical mapping between your conceptual object model and the database schema."

Q1: Do you really define the database schema in XML?

"LINQ to Entities (L2E for short from now on) will actually use LINQ to SQL to create the dynamic SQL statements..."

Q2: I have not come across anything to that effect. Can you please post a relevant link?

Thanks.


2. Kevin Hoffman left...
Thu 17 Aug 06 6:52 pm

There are multiple comments from the folks at MS indicating that vNext is layered deliberately so that you can work directly with the map provider, or with entities - all of it is turned into dynamic SQL statements at the bottom end using the same technology that turned DLINQ expressions into SQL statements.


3. robocop left...
Fri 18 Aug 06 11:47 am

I tried DLINQ download a while back, and I recall it also had entity inheritance.


4. Kevin Hoffman left...
Fri 18 Aug 06 1:30 pm

If it had entity inheritance, it was only possible through manually editing the C# classes generated by SQLmetal. If I recall, that was more painful than root canal.


5. Pablo Castro left...
Fri 18 Aug 06 2:11 pm :: http://blogs.msdn.com/adonet

Regarding this: "LINQ to Entities (L2E for short from now on) will actually use LINQ to SQL to create the dynamic SQL statements..."

I'd like to clarify this a bit: LINQ to Entities in layered on top of the rest of the Entity Framework stack; specifically on top of the object services layer. LINQ to Entities translates LINQ expressions into the canonical query trees used throughout the Entity Framework and then simply hands out the tree to the query pipeline for mapping and sql-generation. LINQ to Entities and the Entity Framework do not depend or use LINQ to SQL for sql-generation or anything else.

-pablo


6. Kevin Hoffman left...
Fri 18 Aug 06 2:37 pm

Thanks for the clarification Pablo. I've modified the text in the blog post to reflect that LINQ to Entities is not actually using LINQ to SQL for SQL query generation.


7. Alex left...
Fri 18 Aug 06 6:12 pm

I link to your post here: http://www.base4.net/blog.aspx?ID=89


8. Jomo Fisher left...
Wed 30 Aug 06 12:06 pm :: http://blogs.msdn.com/jomo%5Ffisher/

Linq to Sql (DLinq) does support inheritance.

Thanks, Jomo Fisher Microsoft DLinq Team


9. José Angel Yánez left...
Thu 10 Apr 08 2:04 pm

Ok, what if I don't want allow anyone access my tables using SELECT stataments (Which by the way should be the right way to go) but only thru store procedures?. What I realize here is that until ADO.Net doesn't put all that generated queries and automaticly persist it in the database using Store Procs, it's dommed to be slow, and prone to wrong data access practices. Besides from my perspective noone (Not even LINQ) should ever use SELECT's in the application not matter how because is a clear and very open security and performance flaw, when is Microsoft forget about small databases, no one buys a 3000$ Visual Studio and pay others 30000$ for an small application.


10. Jiang left...
Tue 12 Aug 08 1:58 pm

For small to medium size application, it's a total waste to go ORM. Just give me the simple Typed Datasets and Tableadapter. It's more efficient and quick to develop.

Why Microsoft care about separating out the coupling between application and SQL server. They are selling SQL server after all. It's like a total suicide to me. If I am the boss at Microsoft, I will dictate that all my application frameworks will tie as close to SQL server as possible and make SQL server the best in the world. Along that path, I will make application development as easy and as fast as possible. But now what we are seeing is Microsoft is spreading itself too thin and forgetting how it has gotten what it is today.


11. Venkat left...
Wed 29 Jul 09 1:35 pm

Kevin,

Can you give me the comparison between LINQ to SQL, LINQ to XML, LINQ to Entities.

Like you gave in the current post.. comparison chart.

Thanks


Tag Related Posts

Smart, Deep Property Notifications in CLINQ v2.0

Tue 07 Oct 08 1:15 P GMT-05
tags:          

CLINQ v1.1.0.0 Released!

Fri 02 May 08 5:38 P GMT-05
tags:          

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

Unexpected and Unsafe behavior in LINQ

Wed 14 Nov 07 8:09 P GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Continuous LINQ

Mon 06 Aug 07 1:21 P GMT-05
tags:    

Dynamic, Observable LINQ Views

Tue 31 Jul 07 1:21 A GMT-05
tags:        

Orcas EDM Wizard Patched

Fri 27 Apr 07 11:56 A GMT-05
tags:      

Installing Orcas Beta 1 - VMware Style

Mon 23 Apr 07 12:16 P GMT-05

Core Data - Almost too Easy?

Wed 18 Apr 07 2:23 P GMT-05

Visual Studio "Orcas" - March CTP is Available

Wed 28 Feb 07 12:28 P GMT-05
tags:            

Objective-C Categories vs C# 3.5 Language Extensions

Mon 26 Feb 07 1:05 P GMT-05
tags:                

WPF Bumper Stickers

Tue 12 Dec 06 7:32 P GMT-05

ASP.NET vs Ruby on Rails : Round 2 (Agility)

Thu 05 Oct 06 11:02 A GMT-05
tags:                      

ASP.NET vs Ruby on Rails : Round 1

Wed 04 Oct 06 1:37 P GMT-05
tags:                

First Impressions of Windows Vista RC1

Thu 07 Sep 06 1:30 P GMT-05
tags:                      

Localizing a WPF Application

Tue 22 Aug 06 11:39 A GMT-05
tags:            

WPF Slide Show and Photo Album

Fri 18 Aug 06 6:48 P GMT-05

Is Windows Workflow Foundation Too Complex?

Fri 18 Aug 06 12:15 P GMT-05

ADO.NET Entity Framework Announced Today!

Wed 16 Aug 06 11:08 A GMT-05

Extending your Applications with MAF/VSTA - Part III

Fri 21 Jul 06 12:30 A GMT-05
tags:            

Extending your Application with MAF/VSTA AddIns

Thu 20 Jul 06 1:51 A GMT-05
tags:            

July CTP is out... yay.

Tue 18 Jul 06 6:34 P GMT-05
tags:    

.NET Framework 3.0 June CTP is out!

Fri 23 Jun 06 6:23 P GMT-05
tags:                

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags:                  

Language Extensions in C# 3.0

Wed 31 May 06 2:17 P GMT-05