Transcript
  • 8/10/2019 Entity Framework 1.3

    1/29

    1 Copyright 2013 Infogain Corporation. All rights reserved.

    November 15, 2014Copyright 2013 Infogain Corporation. All rights reserved.

    Entity FrameworkAshok Sharma

  • 8/10/2019 Entity Framework 1.3

    2/29

    November 15, 2014

    Copyright 2010 Infogain Corporation. All rights reserved.3

    Why ORM

  • 8/10/2019 Entity Framework 1.3

    3/29

    4 Copyright 2013 Infogain Corporation. All rights reserved.

    Dr. EF. Codd

  • 8/10/2019 Entity Framework 1.3

    4/29

    5 Copyright 2013 Infogain Corporation. All rights reserved.

    DELETESELECT

    COUNT

    LEFTUNION MIN/MAX

    JOIN

    HAVINGORDER BY

    RIGHT

    INSERT

    WHERE

    UPDATE

    Then we code in SQL

  • 8/10/2019 Entity Framework 1.3

    5/29

    6 Copyright 2013 Infogain Corporation. All rights reserved.

    SqlCommand oCmd;

    SqlDataReader oDR;

    string connString = ConfigurationManager.

    ConnectionStrings["OracleConnString"].

    ConnectionString;

    SqlConnection conn = new SqlConnection(connString);

    string selectQuery;

    int _returnValue = 0;

    selectQuery = SELECT COUNT(OrderID) from Orders where

    EmployeeID=@EmpID;

    conn.Open();

    oCmd = new SqlCommand(selectQuery, conn);

    oCmd.Parameters.Add(@EmpID, SqlDbType.Int);

    oCmd.Parameters*@EmpID+.Value = employeeID;

    oDR = oCmd.ExecuteReader();

    if (oDR.Read())

    _returnValue = oDR.GetInt32(0);

    oDR.Close();

    conn.Close();

    ADO.NET code

  • 8/10/2019 Entity Framework 1.3

    6/29

    7 Copyright 2013 Infogain Corporation. All rights reserved.

    Developers like to work

    with

    Conceptual Models

    Business Objects

    Databases are designed for

    Maintainability Security

    Efficiency

    Scalability

  • 8/10/2019 Entity Framework 1.3

    7/298 Copyright 2013 Infogain Corporation. All rights reserved.

    using (SQLConnection conn = new SQLConnection();

    {

    conn.Open();

    SQLCommand cmd = conn.CreateCommand();

    cmd.CommandText = sp_StoredProc;

    cmd.parameters.AddWithValue(@City, Dallas);

    using (SQLDataReader rdr = cmd.ExecuteReader()){

    while (rdr.read())

    {

    string name = rdr.GetString(0);

    string city = rdr.GetString(1);

    }}

    }

    Powerful, but fragile and time-consuming

    8

    Strings! No compile

    time check or Intellisense

    Parameters loosely bound: --

    Names, types, number of

    not checked until runtimeResults are loosely typed

  • 8/10/2019 Entity Framework 1.3

    8/299 Copyright 2013 Infogain Corporation. All rights reserved.

    ADO.NETADO

    (1996)

    DAO

    (1992)

    RDO

    Application Connectivity Growth

  • 8/10/2019 Entity Framework 1.3

    9/2910 Copyright 2013 Infogain Corporation. All rights reserved.

    The Impedance Mismatch

    Relational

    DatabaseConceptual /

    Business Model

    (Objects)

  • 8/10/2019 Entity Framework 1.3

    10/2911 Copyright 2013 Infogain Corporation. All rights reserved.

    Enough! We need ORM!

  • 8/10/2019 Entity Framework 1.3

    11/2912 Copyright 2013 Infogain Corporation. All rights reserved.

    What is it?

    anAbstraction

    technique for working with relational tables as if they were objects inmemory

    Intention is to hide away the complexity of the underlying tables and give a

    uniform way of working with dataWhy use it?

    Developerproductivity

    Retain database independence

    Note:

    Using an ORM does not mean you must use the ORM!

  • 8/10/2019 Entity Framework 1.3

    12/2913 Copyright 2013 Infogain Corporation. All rights reserved.

    LinqToSQL Entity Framework

  • 8/10/2019 Entity Framework 1.3

    13/29

    November 15, 2014Copyright 2010 Infogain Corporation. All rights reserved.14

    Entity Framework

  • 8/10/2019 Entity Framework 1.3

    14/2915 Copyright 2013 Infogain Corporation. All rights reserved.

    Data access framework

    Supports data-centric applications and services

    Enables programming against a conceptual application model

    Enables independency of any data storage engine or relational schema

  • 8/10/2019 Entity Framework 1.3

    15/29

  • 8/10/2019 Entity Framework 1.3

    16/2917 Copyright 2013 Infogain Corporation. All rights reserved.

    Items described in the EDM are called entities

    Entities have only properties but no behavior

    Entities can have relationships with other entities

  • 8/10/2019 Entity Framework 1.3

    17/2918 Copyright 2013 Infogain Corporation. All rights reserved.

  • 8/10/2019 Entity Framework 1.3

    18/29

    19 Copyright 2013 Infogain Corporation. All rights reserved.

  • 8/10/2019 Entity Framework 1.3

    19/29

  • 8/10/2019 Entity Framework 1.3

    20/29

    21 Copyright 2013 Infogain Corporation. All rights reserved.

    The model doesn't have any knowledge of the data storage

    The backend data storage has no impact on your model or your code

    Uses a provider model to interact with the data storage

    Available providers:

    SQL Server

    Oracle

    MySQL

    Many more

  • 8/10/2019 Entity Framework 1.3

    21/29

    22 Copyright 2013 Infogain Corporation. All rights reserved.

    Automatically generates classes from the model

    Takes care of all of the database connectivity

    Provides common query syntax for querying the model

    Provides a mechanism for tracking changes to the model's objects

  • 8/10/2019 Entity Framework 1.3

    22/29

  • 8/10/2019 Entity Framework 1.3

    23/29

    24 Copyright 2013 Infogain Corporation. All rights reserved.

    As metadata in XML

    3 files

    .CSDL (Conceptual Schema Definition Language)

    conceptual abstraction which is exposed to the application.

    SSDL (Store Schema Definition Language)

    The mapping with your RDBMS data structure.

    MSL (Mapping Schema Language) connects the CSDL and SSDL.

  • 8/10/2019 Entity Framework 1.3

    24/29

    25 Copyright 2013 Infogain Corporation. All rights reserved.

    The Three Parts of the Model:

    The image is taken from Julia Lermans book Programming Entity Framework, 1st Edition

  • 8/10/2019 Entity Framework 1.3

    25/29

    26 Copyright 2013 Infogain Corporation. All rights reserved.

  • 8/10/2019 Entity Framework 1.3

    26/29

    27 Copyright 2013 Infogain Corporation. All rights reserved.

    EF automatically creates a set of classes from the model

    You work with the model through the generated classes

    Every change to the model can change the generated classes

  • 8/10/2019 Entity Framework 1.3

    27/29

    28 Copyright 2013 Infogain Corporation. All rights reserved.

    Queries are built against a data model

    EDM query transform into data storage query Query results materialize into model entities

    The image is

    taken from

    Julia Lermans

    bookProgramming

    Entity

    Framework,

    1st Edition

  • 8/10/2019 Entity Framework 1.3

    28/29

    29 Copyright 2013 Infogain Corporation. All rights reserved.

    1. Database to-Model : The first approach, which has existed since the initial

    release of EF, is the ability to generate your model from a database.

    2. Model-to-Database: generally knows as model-first, it lets developers start with a

    conceptual model and generate their database based on that conceptual model.

    3. Code Only Approach : It provides developers the ability to use the EntityFramework using POCO(Plain Old Class Object) entities and without an EDMX file.

  • 8/10/2019 Entity Framework 1.3

    29/29


Top Related