EF Queries


  Eyal Vardi
  CEO E4D Solutions LTD
  Microsoft MVP Visual C#
  blog: www.eVardi.com
Agenda
                                                                                 LINQ to                Entity    EDM
                                                                                 Entities                SQL

                                                                                                                 Conceptu
                                                                                      Object Services            al Model



                                                                                                                 Mapping
                                                                                   Entity Client Provider

                                                                                                                 Storage
                                                                                                                  Model
                                                                                    ADO.NET Provider




           EF Architecture

           Development Approaches

           Entity Data Model (EDM)

           Object Services

           Querying & Loading Entities




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
EF Architecture
     from c in db.Customers
     where c.City == "London"                        Application
     select
      new { c.Name, c.Phone }


                                     LINQ Query            Objects        SubmitChanges()


                                                                                     Services:
                                                  Entity Framework                   - Change tracking
                                                            EDMX                     - Concurrency control
                                                                                     - Object identity
                                                    ADO.NET Provider


                                       SQL Query          Rows             SQL or
                                                                           Stored
                                                                           Procs
      select Name, Phone
      from customers
      where city = 'London'
                                                        SQLServer

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
EF Architecture

           LINQ to Entities                                Entity SQL              EDM
                                                                                 Conceptual
                              Object Services                                      Model

                                                                                  Mapping
                         Entity Client Provider
                                                                                  Storage
                            ADO.NET Provider                                       Model




                                                        SQLServer

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
LINQ to                Entity    EDM
                                                                                 Entities                SQL

                                                                                                                 Conceptu
                                                                                      Object Services            al Model



                                                                                                                 Mapping
                                                                                   Entity Client Provider

                                                                                                                 Storage
                                                                                                                  Model
                                                                                    ADO.NET Provider




     Querying & Loading Entities




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Entity SQL & LINQ to Entities
           LINQ to Entities - The LINQ dialect that
            enables typed queries against the model.
           Entity SQL - The second way of querying the
            object model.
                  Entity SQL is string-based.
                  Can be used to retrieve data at low level, directly using the
                   Entity Client.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Entity SQL Queries
           The SqlQuery method on DbSet allows a raw
            SQL query to be written that will return entity
            instances.
                  The returned objects will be tracked by the context just as
                   they would be if there were returned by a LINQ query.
                  The query is not executed until the results are enumerated




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
SQL Queries for none Entity Types

           The results returned from SqlQuery on
            Database will never be tracked by the context
            even if the objects are instances of an entity
            type.




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Stored Procedures




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
SP Sample




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Updating Data with SP




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
Functions




© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

Entity Framework - Queries

  • 1.
    EF Queries Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  • 2.
    Agenda LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider  EF Architecture  Development Approaches  Entity Data Model (EDM)  Object Services  Querying & Loading Entities © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 3.
    EF Architecture from c in db.Customers where c.City == "London" Application select new { c.Name, c.Phone } LINQ Query Objects SubmitChanges() Services: Entity Framework - Change tracking EDMX - Concurrency control - Object identity ADO.NET Provider SQL Query Rows SQL or Stored Procs select Name, Phone from customers where city = 'London' SQLServer © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 4.
    EF Architecture LINQ to Entities Entity SQL EDM Conceptual Object Services Model Mapping Entity Client Provider Storage ADO.NET Provider Model SQLServer © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 5.
    LINQ to Entity EDM Entities SQL Conceptu Object Services al Model Mapping Entity Client Provider Storage Model ADO.NET Provider Querying & Loading Entities © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 6.
    Entity SQL &LINQ to Entities  LINQ to Entities - The LINQ dialect that enables typed queries against the model.  Entity SQL - The second way of querying the object model.  Entity SQL is string-based.  Can be used to retrieve data at low level, directly using the Entity Client. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 7.
    Entity SQL Queries  The SqlQuery method on DbSet allows a raw SQL query to be written that will return entity instances.  The returned objects will be tracked by the context just as they would be if there were returned by a LINQ query.  The query is not executed until the results are enumerated © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 8.
    SQL Queries fornone Entity Types  The results returned from SqlQuery on Database will never be tracked by the context even if the objects are instances of an entity type. © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 9.
    Stored Procedures © 2010E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 10.
    SP Sample © 2010E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 11.
    Updating Data withSP © 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 12.
    Functions © 2010 E4DLTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il