Docstoc

Introducing the Entity Framework

Document Sample
Introducing the Entity Framework Powered By Docstoc
					Introducing the Entity
Framework




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Objectives
• Learn about how object-relational mapping
  works to provide data access
• Understand what the Entity Framework is and
  how it can provide robust data access services to
  applications
• See how to build a database-first Entity Data
  Model using Visual Studio
• Explore the various Entity Framework APIs and
  tools

              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Agenda
• Object-Relational Mapping (ORM) and
  Data Access
• The Entity Framework and Data Model
• Building a Simple Application using Entity
  Framework
• Entity Framework APIs and Tools




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Object-Relational Mapping (ORM)
and Data Access
• Just about all applications make use of data
• Microsoft has a long history of providing data access
  technologies
• But they have problems
     Too close to the metal
     Use generic data objects
•   Data access code is usually a lot of code
•   Differences between objects and database
•   Entity Framework bridges the gap
•   Terminology: data store and database
                  Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
Object/Relational Differences
• Inherent problems application has to overcome
   Needs lots of code somewhere
   Big impact on application design, effort, and
    performance
• Will discuss in the context of a relational
  database




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Data Type Differences
• Many kinds of data types
• SQL Server 2008 R2 data types:
    bigint       binary           bit                       char                 CLR
    date         datetime         datetime2                 datetimeoffset       decimal
    float        hierarchyid      image                     int                  money
    nchar        ntext            numeric                   nvarchar             real
    rowversion   smalldatetime    smallint                  smallmoney           sql_variant
    table        text             time                      timestamp            tinyint
    varbinary    varchar          uniqueidentifier
•   Some have direct equivalents in .NET
•   Database types often have constraints
•   Binary data in the database can be anything
•   Dates and times are another issue
                     Learn More @ http://www.learnnowonline.com
                        Copyright © by Application Developers Training Company
Relationship Difference
• Databases use foreign key constraints
• .NET objects use object references
• SalesOrderHeader and SalesOrderDetail tables




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Relationship Difference
• Many-to-Many relationships
• In AdventureWorks, customers and addresses




• Not the default objects
                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Inheritance Difference
• .NET supports object inheritance
   No support for multiple inheritance
• Inheritance makes code simpler, maintainable, easier
  to debug
   Model hierarchies of objects
• Relational databases don’t support inheritance
• Some ways to accomplish it
   Table per type
   Table per concrete type
   Table per hierarchy
• Have to manage special constraints
            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Identity/Equality Difference
• How do you tell two things are the same?
• Database: identity of a row is the primary key
   Enforced by a primary key constraint
   Can use either natural or surrogate keys
• Objects in object-oriented application
   Two objects with the same data are not equal
   Two variables, each with reference to different
    objects, are not equal
   Two variables with reference to same object are equal
   Can make a custom definition

               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Handling the Differences
• Can be an lot of work to handle object/relational
  differences
   But at least they are well understood
   Doesn’t mean it’s easy
• Write the code yourself, or use a framework




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Object-Relational Mapping (ORM)
• Programming technique for conversion of
  incompatible types
• Most ORM tools rely on metadata about
  database and objects
• Clean separation of concerns
• Key feature is mapping
   Expresses how an object is related to a table
   ORM uses to manage conversion process
• ORM manages application’s interactions with
  database
               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Benefits to using an ORM
•   Productivity
•   Better application design
•   Code reuse
•   Application maintainability
•   Potential downside: performance
       More complex code
       Means slower performance
       But ORM code is likely to be well tuned
       Data access time may be minimal
• Lots of ORMs available for .NET
                   Learn More @ http://www.learnnowonline.com
                      Copyright © by Application Developers Training Company
Generic Data Objects vs. Entity
Objects
• ADO.NET has long had generic data objects
• Most common:
     SqlConnection and OleDbConnection
     SqlDataAdapter and OleDbAdapter
     DataSet
     DataTable
     SqlDataReader and OleDbDataReader
• Provide a means for accessing data in database
   DataSet and DataTable contain rows and columns


               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Generic Data Objects
• Problems with Generic Data Objects
   Strong coupling between application and database
   Loose typing
   Object interactions
• Problems persist despite long use
   Every application has to deal with them
   But it’s been the best we’ve had, other than custom
    objects




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Entity Data Objects
• Big improvement over what came before
   Instead of using a generic DataSet, use Customer,
    Order, and OrderDetail
   Navigation properties to access related data
   Fit into object-oriented application
• Object interfaces can mirror tables and fields
   Or be completely different
   Or something in between
• Need a way to map objects to database


               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Benefits of Entity Data Objects
•   Strong typing
•   Compile-time checking
•   Persistence ignorance
•   Productivity




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Data Programming Against a Model
• Will have to change how you think about data in
  an application
   No longer interacting directly with the database
   Instead working with objects
   Very different paradigms
• The model is not the database!
• Generic data objects require lots of code
• Entity data objects mean querying a schema that
  reflects business model
• ORM eliminates context shift in code
               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Agenda
• Object-Relational Mapping (ORM) and Data
  Access
• The Entity Framework and Data Model
• Building a Simple Application using Entity
  Framework
• Entity Framework APIs and Tools




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
The Entity Framework and Data
Model
• Complete ORM for .NET applications
• Microsoft’s new core data access technology
   Big commitment from the company
• Initial release didn’t gain much traction
• Version 4.1 is a mature product
   Substantial commitment and innovation
• Do we need a new data access technology?
   Older technologies have run their course
   Modern applications have to focus on domain model
• Version numbers
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Entity Data Model
• Integral part of Entity Framework
• Client-side model different from database schema
• Describes structure of entity data objects
• Take the database schema and reshape it into
  objects
• Defined by three chunks of XML in a single .edmx file
     Conceptual model
     Storage model
     Mapping


            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
LINQ to SQL
• Similar to Entity Framework
   Both use LINQ to query a database using a data
    model
• Developed independently at Microsoft
   LINQ to SQL grew out of LINQ team
   Entity Framework grew out of Data Programmability
    team
• Soon Microsoft had two new data access
  technologies
• LINQ to SQL moved, and lost out to Entity
  Framework
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Why Use Entity Framework?
• Part of the .NET Framework and integrated into
  Visual Studio
• Uses LINQ as the primary query language
• Independent of the data source
• Microsoft’s primary data access strategy




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
When Should You Not Use Entity
Framework or an ORM?
• Entity Framework is not a panacea
   Not an instant data access panacea
   Isn’t appropriate for every kind of application
   Doesn’t do bulk inserts




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Agenda
• Object-Relational Mapping (ORM) and Data
  Access
• The Entity Framework and Data Model
• Building a Simple Application using Entity
  Framework
• Entity Framework APIs and Tools




            Learn More @ http://www.learnnowonline.com
               Copyright © by Application Developers Training Company
Building a Simple Application using
Entity Framework
• Compelling reason to use Entity Framework is its
  integration with Visual Studio
• Sample is based on AdventureWorksLT
   Ten tables with data about customers, products, and
    orders
   Complex enough to exercise Entity Framework
• Will build sample as a class library
   Easy to reuse in multiple applications
   Not absolutely necessary
• Mostly create simple console applications
               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Using the Entity Objects
• Time to use the entity data objects in an
  application
• Simple console application
• Need to do three things to use the class library
   Add a reference to the class library
   Add a reference to System.Data.Entity
   Add the Entity Framework connection string
• And, of course, write some code!


              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Agenda
• Object-Relational Mapping (ORM) and Data
  Access
• The Entity Framework and Data Model
• Building a Simple Application using Entity
  Framework
• Entity Framework APIs and Tools




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Entity Framework APIs and Tools
• Entity Framework is a complex set of APIs and
  design tools
• Need a basic understanding of what each of the
  Entity Framework components do
   Road to deep understanding




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
                                                                  Application
Architecture                                             Entity
                                                          SQL
                                                                               LINQ to
                                                                               Entities
                                                         Query                  Query
     Entity Data                                                                          Results
                                      Entity
       Model                           SQL
                                      Query                           Object Services
     Conceptual
       Model                                                              Command
                                                                                          Entity
                                                                            Tree
                                                                                           Data
                                                                                          Reader
         Mapping




                                                     EntityClient Data Provider

                                                                          Command
                                                                            Tree          DB Data
      Storage                                                                             Reader
       Model
                                                      ADO.NET Data Providers



                                                                  Data Store
                   Learn More @ http://www.learnnowonline.com
                      Copyright © by Application Developers Training Company
Entity Data Model Designer
• Part of Visual Studio
• Lets you work with model graphically rather than
  as raw XML
• First saw as part of the AdventureWorksLibrary
  project
• Doesn’t support all Entity Data Model features
• Lets you map tables, views, stored procedures
  and functions to entities
   Can update the model from the underlying database


              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Design Methodologies Using the
Designer
• Two broad ways to build a data application
   Top-down design
   Bottom-up design
• Neither is necessarily better than the other
• Entity Framework supports both
• Provides three ways to build an application
   Database-first design
   Model-first design
   Code-first design


               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Code Generation Using T4
• Entity Data Model designer generates code
• Uses Visual Studio’s Text Template
  Transformation Toolkit (T4)
   Generates classes in designer code file
   Standard within Visual Studio
• Can customize the templates
   Not for the faint of heart




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Object Services
• Manages all of the objects in Entity Framework
• Key task is to handle differences between
  database and objects
• Provides and EntityObject class and manages
  objects that inherit from it
• Four primary tasks
     Exposes an API
     Performs query transformations
     Performs object materialization
     Tracks state of objects and updates data store
                 Learn More @ http://www.learnnowonline.com
                    Copyright © by Application Developers Training Company
Entity Client
• Communicates with the database and manages data
  conversions
    Must know structure of database as well as entity data objects
    Uses the full Entity Data Model
• Connects to database through data provider
    Standard API
    Same providers as in ADO.NET, updated for Entity Framework
• Can work with any data store with a provider
• SQL Server versions
• Don’t have to use Object Services to query
    Can use Entity SQL directly to Entity Client


                   Learn More @ http://www.learnnowonline.com
                      Copyright © by Application Developers Training Company
Entity Framework Services
• Entity Framework provides various services to the
  application
    Microsoft expands with each new release
    Expands usability of Entity Framework
• Support for Plain Old CLR Objects (POCO)
    Don’t inherit from EntityObject
    Supports lots of programming styles and methodologies
    ObjectContext obtains relationship and change tracking
     information
• Data Binding in .NET Applications
    Long a standard technique for connecting data to UI controls
    Entity Framework fully supports data binding

                  Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
Learn More!
• This is an excerpt from a larger course. Visit
  www.learnnowonline.com for the full details!




           Learn More @ http://www.learnnowonline.com
              Copyright © by Application Developers Training Company

				
DOCUMENT INFO
Description: Learn about how object-relational mapping works to provide data access and understand what the Entity Framework is and how it can provide robust data access services to applications.