Sunday, 4 November 2012

Difference between Linq2Sql and Entity Framework?


Differences between Linq to Sql and ADO.NET Entity Framework?

Linq to sql is no support  for Conceptual modelà
Linq to Sql : no Conceputal Data model, no storage schema, no mapping schema.(csdl).
Here
In case of LINQ to SQL-à database objects are application classes so there is  tightly coupling.
Between
LINQ to SQL -à ORM model ---to application objects.     [emp(RDBMS)----à dbo.emp];
Why because along with Particular  schema-à dbml is generated.
Like
<Table Name="dbo.Categories" Member="Categories">             //  1.
    <Type Name="Category">
      <Column Name="CategoryID" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
      <Column Name="CategoryName" Type="System.String" DbType="NVarChar(15) NOT NULL" CanBeNull="false" />
      <Association Name="Category_Product" Member="Products" ThisKey="CategoryID" OtherKey="CategoryID" Type="Product" />   //  2.
    </Type>
  </Table>
Every property is tightly coupled with particular table –linq to sql.
      [Table(Name="dbo. Categories ")]
      public partial class Customer : INotifyPropertyChanging
      {
            private string _CustomerID;  
      [Column(Storage="_CustomerID", DbType="NChar(5) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]                                   // 3.
            public string CustomerID
            {
                  get
                  {
                        return this._CustomerID;
                  }
                  set
                  {
                        if ((this._CustomerID != value))
                        {
                              this.OnCustomerIDChanging(value);
                              this.SendPropertyChanging();
                              this._CustomerID = value;
                              this.SendPropertyChanged("CustomerID");
                              this.OnCustomerIDChanged();
                        }
                  }
            }
Linq to Sql

1.       No Models support: single file only

<Table Name="dbo.Categories" Member="Categories">
<Type Name="Category">
<Column Name="CategoryID" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
<Column Name="CategoryName" Type="System.String" DbType="NVarChar(15) NOT NULL" CanBeNull="false" />
<Column Name="Description" Type="System.String" DbType="NText" CanBeNull="true" UpdateCheck="Never" />
<Column Name="Picture" Type="System.Data.Linq.Binary" DbType="Image" CanBeNull="true" UpdateCheck="Never" />
<Association Name="Category_Product" Member="Products" ThisKey="CategoryID" OtherKey="CategoryID" Type="Product" />
    </Type>
  </Table>
   
Not support -à Conceputal model
1.       One database—entity(customer)---tightly coupled with –one particular schema.-->dbo

Not support-à Mapping Schema

2.       Association-à these mappings are 1 to 1 relationships.
Not supportà Storage Schema
3.       Every Field tightly coupled with -à particular Storage model.
---Open the .dbml-àwith xml Editor(northwind.dbml) and Design view (northwinddesignder.cs)
All ther are included as a single file--à Northwind.dmlàopen with xml Editor
2.       Rapid application Development .

Application---Linq to sql—ADO.NET---- SQL SERVER.

Linq to SQL generate objects(in front end application) which are directly map to database tables so here object are equal to Relational table.

ORM-mapper maps a relavent designer code in northwind.designer.cs surface.
As par Table Relationships and stored procedures which generates apporiate code in LINQ-to-sql designer.
And
EntityRef---tells --à Reference tablesà FK
EntitySet---tells---à

[Table(Name="dbo.Products")]
public partial class Product : INotifyPropertyChanging,
{
private EntitySet<Order_Detail> _Order_Details; this Attribute are

field--à shared by Product and Order_Details-à So EntitySet.

which is shared by multiple entities which share a single attribute.

Order_Details--à
           
private EntityRef<Category> _Category;---à field reference CategoryID.
           
private EntityRef<Supplier> _Supplier;--à field reference SupplierID.
[Association(Name="Product_Order_Detail", Storage="_Order_Details", ThisKey="ProductID", OtherKey="ProductID")]
            public EntitySet<Order_Detail> Order_Details
            {
                  get
                  {
                        return this._Order_Details;
                  }
                  set
                  {
                        this._Order_Details.Assign(value);
                  }
            }
           
 [Association(Name="Category_Product", Storage="_Category", ThisKey="CategoryID", OtherKey="CategoryID", IsForeignKey=true)]
public Category Category
            {
                 
                 
            }
 [Association(Name="Supplier_Product", Storage="_Supplier", ThisKey="SupplierID", OtherKey="SupplierID", IsForeignKey=true)]
            public Supplier Supplier
            {
                 
            }
           

Entity Framework:

In case of Entity framework -----in single .edml file  3 parts are sperated.
Supports à Conceptual model.

  <edmx:ConceptualModels>------------only entities.
Supports -> Storage model
  

Supports-à Mapping model(conceptual and storage).
This specifies <edmx:Mappings>
Conceputal type(EntityType) to Storage type.
<edmx:Mappings>
<EntitySetMapping Name="Employees">
<EntityTypeMapping TypeName="IsTypeOf(NORTHWNDModel.Employees)"><MappingFragment StoreEntitySet="Employees">

                                          Entity Framework

Works with Conceptual model of database.----loosely coupled.

One Conceptual model-à one conceptual layer and then multiple mapping layers that point to mulitple database




                  

                              Conceptual model

Oval:         MS-SQL SERVER
Oval:      Oracle Oval:        My Sql server
 

Enterprise Development:
Yes, In linq to sql-à DataContext--à in Entity Framework---- ObjectContext.
In linq to sql-à The mapping of the logical schema into the physical schema that defines how the data is structured and stored on the disk is the job of the database system and client side data access mechanisms are shielded from it as the database exposes the data in the way specified by its logical schema(at client side).—tightly coupled.
Using LINQ to SQL, developers can write code directly against the storage schema using the same LINQ programming pattern as previously described for in-memory collections, Entities. The Entity data model (EDM) specifies the conceptual model of the data via the Entity-Relationship data model.

      public System.Data.Linq.Table<Product> Products
            {
                  get
                  {
                        return this.GetTable<Product>();
                  }
            }
      [Table(Name="dbo.Products")]
      public partial class Product {}
  using (NorthwindDataContext context=new NorthwindDataContext())
            {

                IQueryable<Product> s = context.GetTable<Product>();
                Console.WriteLine(s.Count<Product>());                           

            }
--working with storage schema(AGANIST DATABASE).in LINQ TO SQL.*************** 

Means data models(Product) are tightly map to Database tables.aganist works with database(directly).


Ex:
  using (NorthwindDataContext context=new NorthwindDataContext())
            {

                Customer firstCustomer = context.Customers.First();

                Console.WriteLine("Customer name : {0}", firstCustomer.ContactName);

                var orders = firstCustomer.Orders;

                foreach (var order in orders)
                {
                    Console.WriteLine("Order ID : {0} dated : {1}", order.OrderID, order.OrderDate.Value.ToLongDateString());
                }



            }
Output:


Here firstCustomer object all the data from database(EntitySet, EntityRef ).



In Entity framework ---developers work with Conceptual schema which maps the logical schema and Physical schema.
Ex:
  using (NorthwindDataContext context=new NorthwindDataContext())
 {

 Customer firstCustomer = context.Customers.First();

  Console.WriteLine("Customer name : {0}", firstCustomer.ContactName);

                var orders = firstCustomer.Orders;

                foreach (var order in orders)
                {
Console.WriteLine("Order ID : {0} dated : {1}", order.OrderID, order.OrderDate.Value.ToLongDateString());
                }

            }






Here firstCustomer object works with only the relavent --> EntitySet data only.
Working with CONCECPTUAL schema(NOT AGANIST DATABASE).in LINQ TOSQL.*************** 

Designer changes
 linq to sql(direction Connection)

why because no conceptual model in linq to sql.

Entity Framework

linq to sql direct mapping
in entity framework is generated as unified model to work with any database.




No comments:

Post a Comment