MSDN Blog Schweiz

Aktuelle Microsoft-News, Anleitungen, Downloads, Tools und Veranstaltungen für Schweizer Entwickler.

Entity Framework vs LINQ TO SQL

Entity Framework vs LINQ TO SQL

  • Comments 2

This table want to summarize some of the big differences between LINQ To SQL and Entity Framework.

LINQ TO SQL

ENTITY FRAMEWORK

-     Works only with SQL SERVER

-  Database agnostic (3rd party providers already available)

-     Limited Mapping Capabilities (mainly 1:1 mapping with database schema with the exception of one kind of inheritance ( Table per Hierarchy) where you have a single table used to represent the parent type and all child types and types are differentiated using a discriminator column in the database

-  More mapping scenarios supported:

- Table per Hierarchy (as LINQ To SQL)

- Table per Concrete Type: a separate table for each class or type in the hierarchy

- Table per SubClass: a hybrid approach using a shared table for information about the base type and separate tables for information about the derived types (entity splitting)

 

-    Support complex types: a type nested within a larger Entity and doesn’t have its own separate entity (for example type Address with street, city, state, country, …)I

 

 Eager Loading / Immediate Loading: you query for an object and all related objects are also returned.

LINQ TO SQL

ENTITY FRAMEWORK

 

Support at context level

 

DataLoadOptions dataLoadOptions = new DataLoadOptions();

dataLoadOptions.LoadWith<Customer>(c => c.Order);

dataLoadOptions.LoadWith<Order>(c => c.OrderDetail);

db.LoadOptions = dataLoadOptions;

 

List<Customer> customers = db.Customer.ToList();

 

Support at query level

 

Customer firstCustomer = db.Customer.Include("Order").Include("OrderDetails").First();

 

Deferred / Lazy Loading: allows you to automatically load objects on demand if and when you attempt to access the object that is not yet loaded

LINQ TO SQL

ENTITY FRAMEWORK

 

Automatic Deferred Loading enabled per default

 

In the example below, you are really only querying for  objects from the Products table – however, because Deferred Loading is enabled, you can access prod.SalesOrderDetail

 

var products = 
  from prod in db.Products
where prod.Color == "Blue"
select prod;
 
foreach (Product prod in products)
{
foreach (SalesOrderDetail detail
in prod.SalesOrderDetail)
{
    // do something with detail
}
}
Is it possible to disable deferred loading… 
db.DeferredLoadingEnabled = false;

 

Does not provide an automatic deferred loading mechanism

 

var products = 
   from prod in db.Product
   where prod.Color == "Blue"
   select prod;
foreach (Product prod in products)
{
  // avoids unnecessary queries
  if (!prod.SalesOrderDetail.IsLoaded)
  {
    prod.SalesOrderDetail.Load();
 } 
  foreach (SalesOrderDetail detail in 
prod.SalesOrderDetail)
{
     // do something with detail
  }
}

 


 

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