intro entity framework

Author: bfx7s

Post on 30-May-2018

221 views

Category:

Documents


0 download

Embed Size (px)

TRANSCRIPT

  • 8/14/2019 Intro Entity Framework

    1/31

    Shawn WildermuthWildermuth Consulting Services

    Microsoft MVP (C#), MCSDhttp://wildermuthconsulting.com

    The Entity FrameworkAn Introduction

  • 8/14/2019 Intro Entity Framework

    2/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Who Am I?

    Shawn Wildermuth [email protected]

    Wildermuth Consulting Services

    http://wildermuthconsulting.com C# MVP, MCSD, MCT, INETA Speaker Book Author

    Pragmatic ADO.NET MCTS Training Kit (Exam 536) MCPD Training Kits (Exams 547, 548 & 549) Programming WPF (Authored WPF/E Appendix only) Prescriptive Data Architectures Upcoming

    Silverlight Tour (www.silverlight-tour.com)

  • 8/14/2019 Intro Entity Framework

    3/31

  • 8/14/2019 Intro Entity Framework

    4/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Agenda

    The Problem Statement

    The Entity Framework

    Entity Data Model

    Entity Client

    Object Services

    Entity SQL

    LINQ for Entities

  • 8/14/2019 Intro Entity Framework

    5/31

    Wildermuth Consulting Services Introducing the Entity Framework

    The Problem

    Analysts see the world in OR Diagrams:

  • 8/14/2019 Intro Entity Framework

    6/31

    Wildermuth Consulting Services Introducing the Entity Framework

    The Problem

    Developers see the world in class diagrams:

  • 8/14/2019 Intro Entity Framework

    7/31Wildermuth Consulting Services Introducing the Entity Framework

    The Problem

    DBAs see the world in ERDs

  • 8/14/2019 Intro Entity Framework

    8/31Wildermuth Consulting Services Introducing the Entity Framework

    The Problem

    Impedence mismatch of different models:

    RelationalModels

    Multiple Data

    Services (UDM,SMDL)

    OO Models

    DomainModels

    ApplicationLayering

    Data models Extend up to themid-tier.

    Application models abstractpersistent Stores

  • 8/14/2019 Intro Entity Framework

    9/31Wildermuth Consulting Services Introducing the Entity Framework

    The SolutionThe Entity Framework

    Object Services

    Entity Client

    Entity Data Model

    ADO.NET Providers

    LINQ for Entities

  • 8/14/2019 Intro Entity Framework

    10/31Wildermuth Consulting Services Introducing the Entity Framework

    Demo

  • 8/14/2019 Intro Entity Framework

    11/31Wildermuth Consulting Services Introducing the Entity Framework

    The Entity Framework

    Not about Persistence

    Not an Object-Relational Mapper per se

    Not an Object Database

    Not a Code Generator

    It is about Services

    Object Services

    View Services Persistence Services

  • 8/14/2019 Intro Entity Framework

    12/31Wildermuth Consulting Services Introducing the Entity Framework

    What is an Entity?

    "A thing which can be distinctly identified

    Peter Chin, 1976

    "Any distinguishable person, place, thing, event, or

    concept, about which information is kept

    Thomas Bruce, 1992

  • 8/14/2019 Intro Entity Framework

    13/31Wildermuth Consulting Services Introducing the Entity Framework

    Employee

    EmployeeID = 729742LoginID = pete

    Title = "Developer"

    VacationHours = 0

    ExpenseAccount =

    CarLicenseNum =

    What is an Entity (2)

    Like an object Unlike an object

    Known type

    Distinct identity

    Properties holding scalar values Properties holding references to

    other entities

    No methods

    Entity identity determined from

    entity (aka primary key) References to other entities

    follow stylized patterns

    Lives within a particular entity set

    Employee

    EmployeeID = 729742

    LoginID = pete

    Title = "Developer"

    VacationHours = 0

    ExpenseAccount =

    CarLicenseNum =

    SalesEmployeeEmployeeID = 729742

    LoginID = pete

    Title = "Developer"

    VacationHours = 0

    ExpenseAccount = true

    EmployeeEmployeeID = 294272

    LoginID = adam

    Title = "Dev Lead"

    VacationHours = 0

    Reports Manager

  • 8/14/2019 Intro Entity Framework

    14/31Wildermuth Consulting Services Introducing the Entity Framework

    What is the Entity Data Model?

    The EDM is a schema language for entities:

    Entity type definitions

    Scalar types

    Inheritance of entity types

    Relationship type definitions

    Containers

    Simple and Complex Scenarios Hierarchies

    Star Schemas

    Universal Schemas

    Employee

    Int32 EmployeeID

    String LoginID

    String Title

    Int16 VacationHours

    Manager_Reports

    Manager 0..1

    Reports *

    SalesEmployee

    Bool ExpenseAccount

  • 8/14/2019 Intro Entity Framework

    15/31Wildermuth Consulting Services Introducing the Entity Framework

    Entity Data Model (EDM)

    ConceptualModel Mapping

    StorageModel

  • 8/14/2019 Intro Entity Framework

    16/31Wildermuth Consulting Services Introducing the Entity Framework

    EDM Conceptual Model

    Defines the Entities

  • 8/14/2019 Intro Entity Framework

    17/31Wildermuth Consulting Services Introducing the Entity Framework

    EDM Storage Model

    Description of where/how stored:

  • 8/14/2019 Intro Entity Framework

    18/31Wildermuth Consulting Services Introducing the Entity Framework

    EDM Mapping Layer

    Maps Conceptual to Storage:

  • 8/14/2019 Intro Entity Framework

    19/31Wildermuth Consulting Services Introducing the Entity Framework

    EDM Where are we?

    The model is definedbut

    Where are my classes?

    Where is my project?

    Help Calgon, take me away!

    The EDM is simply the development of the model

    Services are layered on top of the EDM

    Including Building of Objects, Persistence, etc.

  • 8/14/2019 Intro Entity Framework

    20/31Wildermuth Consulting Services Introducing the Entity Framework

    Entity Client

    An ADO.NET Managed Provider for Clients

    Connections, Commands, DataReaders

    Exposes the Entity Model directly as data sourceusing (EntityConnection cn = new EntityConnection(cs))

    using (EntityCommand cmd = cn.CreateCommand())

    {

    cn.Open();

    cmd.CommandText = qry;

    DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess);

    while (rdr.Read())

    {

    Console.WriteLine(rdr[0]);

    }

    }

  • 8/14/2019 Intro Entity Framework

    21/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Entity SQL

    Entity SQL is a dialect of SQL for querying the EDM

    Familiar SQL language

    A full SQL Dialect

    SELECT Contact.FirstName, Contact.LastName

    FROM PersonModel.Contact as Contact

    WHERE Contact.FirstName = 'John

    SELECT VALUE Contact

    FROM PersonModel.Contact as Contact

    WHERE Contact.FirstName = 'John'";

    SELECT TOP 10 VALUE Contact

    FROM PersonModel.Contact as Contact

    ORDER BY Contact.LastName

  • 8/14/2019 Intro Entity Framework

    22/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Entity SQL

    The real power is from model knowledge

    Entity Graphs

    Polymorphic Results

    Real Set Operators

    SELECT VALUE p FROM EntityNorthwindContext.Products as p

    WHERE p.Category.Name = 'Beverages'

    SELECT VALUE p FROM EntityNorthwindContext.Products as p

    WHERE p is DiscontinuedProduct

    (SELECT VALUE p FROM EntityNorthwindContext.Products AS p

    WHERE p.UnitsInStock > 0)INTERSECT /* OVERLAPS, EXCEPT, UNION */

    (SELECT VALUE p FROM EntityNorthwindContext.Products AS p

    WHERE p.UnitsOnOrder > 0)

  • 8/14/2019 Intro Entity Framework

    23/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Object Services

    Used to expose EDM to Code

    Code Generation for Entities

    Object Queries

    Change Monitoring

  • 8/14/2019 Intro Entity Framework

    24/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Code Generation

    New Models by default create an object model

    Entities include data, not behavior

    Generated Code derives from EntityObject by default

    Full Conceptual Model is generated

    Can do IPOCO if needed

    POCO might be supported (i.e. nHibernate) eventually

  • 8/14/2019 Intro Entity Framework

    25/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Code Generation (2)

    Entity Classes:

  • 8/14/2019 Intro Entity Framework

    26/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Code Generation (3)

    Context Class

    Used to represent bundle of service objects

    E.g. Connection, StateManager, etc.

  • 8/14/2019 Intro Entity Framework

    27/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Querying with Classes

    ObjectQuery is used to issue Entity SQL Queries

    Supports polymorphic searchesusing (PersonModel ctx = new PersonModel())

    { ctx.Connection.Open();

    string qry = @"SELECT Contact

    FROM PersonModel.Contact as Contact

    WHERE Contact.FirstName = 'John'";

    ObjectQuery query = new ObjectQuery(qry, ctx);

    ObjectQueryResult results = query.Execute(MergeOption.NoTracking);

    List list = results.ToList();

    }

    using (PersonModel ctx = new PersonModel())

    { ctx.Connection.Open();

    string qry = @"SELECT Contact

    FROM PersonModel.Contact as Contact

    WHERE Contact.FirstName = 'John'";

    ObjectQuery query = new ObjectQuery(qry, ctx);

    ObjectQueryResult results = query.Execute(MergeOption.NoTracking);

    List list = results.ToList();

    }

  • 8/14/2019 Intro Entity Framework

    28/31

    Wildermuth Consulting Services Introducing the Entity Framework

    Modifying Data

    Changes can be made directly to Entities

    Context is used to save changesusing (PersonModel ctx = new PersonModel())

    { // ...

    List contacts = results.ToList();

    Contact firstContact = contacts[0];

    firstContact.Title = "Mr.";

    int changes = ctx.SaveChanges();}

  • 8/14/2019 Intro Entity Framework

    29/31

    Wildermuth Consulting Services Introducing the Entity Framework

    LINQ to Entities

    Language Integrated Query over Entitiesusing (PersonModel ctx = new PersonModel())

    {

    ctx.Connection.Open();

    var results = from c in ctx.Contact

    where c.FirstName == "John"

    select c;

    List contacts = results.ToList();

    theList.DataContext = contacts;

    }

  • 8/14/2019 Intro Entity Framework

    30/31

    Wildermuth Consulting Services Introducing the Entity Framework

    LINQ for SQL?

    Why LINQ for SQL and Entity Framework?

    Its not about code generation!

    Its about gaining intelligence about a data model

    EF is not for RAD/Prototype solutions

    Building an intelligent model is worknot drag-n-drop

    Entity SQL is what makes it better for mature projects

  • 8/14/2019 Intro Entity Framework

    31/31

    Entity Framework Links: My Website (www.wildermuthconsulting.com)

    My Blog (www.adoguy.com)

    Presentation and code available there

    My E-mail ([email protected]) ADO.NET Team Blog (blogs.msdn.com/adonet/)

    Orcas ADO.NET Forums

    (http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=533)