The Entity Data Model

Document Sample
The Entity Data Model Powered By Docstoc
					The Entity Data Model




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Objectives
• Learn how to create and use an Entity Data
  Model
• Explore the raw XML behind the model as a way
  to understand how Entity Framework works
• See how to use stored procedures within an
  Entity Framework model




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Agenda
•   Introduction
•   Creating and Using an Entity Data Model
•   Entity Data Model in the Raw
•   Using Stored Procedures in the Model




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Introduction
• Key link between entity objects and data
   Conceptual model
   Storage model
   Mapping model
• Application design is often at odds with database
  design
• Can work with model in designer
   Or get messy with the XML
• At build, split into three XML files
• Developers don’t have to know the database
              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
The Models within the Model
• Conceptual Model
   The actual Entity Data Model
   Provides what is needed to create data objects
• Storage Model
   Represents the database schemas
   Can be similar to conceptual model
   Decupling of conceptual design from the database
• Mapping Model
   Bridge between conceptual and storage models
   Map object properties to database fields
              Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Agenda
•   Introduction
•   Creating and Using an Entity Data Model
•   Entity Data Model in the Raw
•   Using Stored Procedures in the Model




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Creating and Using an Entity Data
Model
• Can create from scratch using XML
   More productive using the designer
• Three approaches
   Database-first
   Model-first
   Code-first




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Creating a Model Using Database-
First Design
• Directly supported by the Entity Framework
  template
• Wizard lets you
   Reverse-engineer the database schma
   Select tables, views, and stored procedures to include
   Build the model from selected objects




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Multiplicity
• Used with associations
• Number of entities that can be on each end of an
  association
    Sometimes referred to as cardinality
• Three options
    One: 1
    Zero or one: 0..1
    Many: *
• Can combine in various ways, such as
    1:*
    0..1:*
    *:*
                  Learn More @ http://www.learnnowonline.com
                     Copyright © by Application Developers Training Company
Entity Data Model Object Properties
• Typical for Visual Studio
• Use Properties windows to set values of
  properties for various objects
• Model stores some in model’s XML to define a
  model, others guide object creation




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Creating a Model Using
Model-First Design
• Database-first design is common
• Sometimes creating a new database with the
  application
   Could create the database first
   But model-first is another option
   Strictly speaking, Entity Framework has always had
    this option
• Generate Database from Model option
• Entity Designer Database Generation Power Pack

              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Sled Dog Event Registration
                                                                       Team
   Event
                               Race                                    FirstName
   Name        1         *                         1             *
                               Distance                                LastName
   EventDate
                               DogCount                                City
   Sponsor
                               Trail                                   Country
   Location
                                                                       DogCount



                                             Mushing                   Skijoring
                                             Leader1                   Dog1
                                             Leader2                   Dog2
                                                                       Dog3
                                                                       NumberOfLeaders



               Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Explore the Model with the
Model Browser
• Working with large models can be hard in the
  designer
   Can be hard to understand the structure of the model
   Mapping Details windows doesn’t have details of
    storage model
• Overcome these limitations with the Model
  Browser




              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Agenda
•   Introduction
•   Creating and Using an Entity Data Model
•   Entity Data Model in the Raw
•   Using Stored Procedures in the Model




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Entity Data Model in the Raw
• Designer is a graphical view only of the conceptual
  model
   Exposes most, but not all, features
   Ultimately, just a pretty face on the XML
• Need to have a basic understanding of the XML
   Sometimes have to manipulate the XML directly
• Can use any text editor
   But Visual Studio XML editor provides color-coding, well-
    formedness checks, and Intellisense
• Can’t have open in designer and XML simultaneously

                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
XML Definition Languages
• One for each model:
   Conceptual model uses Conceptual Schema Definition
    Language (CSDL)
   Storage model uses Store Schema Definition
    Language (SSDL)
   Mapping model uses Mapping Specification Language
    (MSL)
• Visual Studio includes XML schema files



              Learn More @ http://www.learnnowonline.com
                 Copyright © by Application Developers Training Company
Model Build Mechanics
• .edmx file contains XML for all three models
   Plus support for the graphical designer
• Saves as three files at build:
   Conceptual model to .csdl file
   Storage model to .ssdl file
   Mapping model to .msl file
• By default, stored in project assembly
   Can output three physical files
   Change the MetadataArtifactProcessing property of
    the entity container
               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Agenda
•   Introduction
•   Creating and Using an Entity Data Model
•   Entity Data Model in the Raw
•   Using Stored Procedures in the Model




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Using Stored Procedures in the
Model
• Entity Framework can automatically generate all
  code needed to do CRUD
• Not necessarily ideal from DBA’s perspective
   Job is to make sure data is secure, performs well, is
    scalable, and meets needs of the application
   Dynamic SQL complicates the job
• Stored procedures vs. dynamic code
   Pick one or the other and go for it
• Entity Framework does both well
   Current version generates good code, mostly
               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Ways to Use Stored Procedures
• Few different ways
   Replace an entity’s insert, update, and delete
    functions to use stored procedures
   Use stored procedures that return data to entities are
    scalar values
• Entity Framework uses terms stored procedure
  and function interchangeably




               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Updating the Model from the
Database
• Have to include stored procedures in the model
  before you can use them
   Only imported tables and views
• Entity Framework supports updating the model from
  the database
• Will use several stored procedures
   InsertCustomer, UpdateCustomer, and DeleteCustomer
   GetCustomersByRegion
   GetProductListByCategory
   GetProductCategoryAveragePrice

               Learn More @ http://www.learnnowonline.com
                  Copyright © by Application Developers Training Company
Using Stored Procedures to
Change an Entity
• One of the most common ways to use stored
  procedures
• Replaces Entity Framework default behavior for
  an entity
• Will use the Customer object




             Learn More @ http://www.learnnowonline.com
                Copyright © by Application Developers Training Company
Using Stored Procedures that
Read Data
• Use stored procedures that read data in the model
   Materialize an entity object
   Random sets of data
• GetCustomersByRegion returns all of the fields of the
  Customer table
• GetProductListByCategory returns fields from the
  Product and ProductCategory tables
• Complex type
   Neither an entity or scalar type
   Use Model Browser to work with

                Learn More @ http://www.learnnowonline.com
                   Copyright © by Application Developers Training Company
Using Stored Procedures that
Return a Scalar Value
• Another option is to retrieve a scalar value from
  a stored procedure
• Dirty little secret
   Stored procedure still returns a result set
   Entity Framework extracts the value, sort of




               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 about Entity Framework on
  SlideShare:
   Introducing the Entity Framework




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

				
DOCUMENT INFO
Shared By:
Tags: entity, data, model
Stats:
views:27
posted:9/28/2012
language:English
pages:25
Description: Learn how to create and use an Entity Data Model, explore the raw XML behind the model as a way to understand how Entity Framework works, and see how to use stored procedures within an Entity Framework model.