intro to entity framework
DESCRIPTION
Intro to Entity Framework. A Reconnaissance (No Driving Yet) Tom Perkins. Objectives. Last Session: We created a small Entity Framework application step-by-step and displayed data in a GridView from a database. This Session: We’ll again create a small Entity Framework application. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/1.jpg)
Intro to Entity Framework
A Reconnaissance(No Driving Yet)
Tom Perkins
![Page 2: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/2.jpg)
Objectives
• Last Session: – We created a small Entity Framework application step-
by-step and – displayed data in a GridView from a database.
• This Session: – We’ll again create a small Entity Framework application.– We’ll look at the components in more detail for the
purpose of gaining familiarity with what the Framework produces.
• Work along or watch!
![Page 3: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/3.jpg)
A Different Programming Perspective
ApplicationEntity Data
Model(EDM)
Data Store
Developer
bridge
Focus on business objectsFocus on tables, stored Procedures, views, etc.
![Page 4: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/4.jpg)
EDM is a “concept”Entity Data Model
(EDM)
Implemented as:
EDMX File
Design timeRun time
3 XML Files
![Page 5: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/5.jpg)
XML Files
Entire Model
Entity Data Model Schema
Mappings
Database Objects Schema
*.CSDL
*.MSL
*.SSDL
*.EDMX
Design Time Run Time
Compile
![Page 6: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/6.jpg)
What Entity Framework Does
• Generates classes from model• Updates classes as model changes• Handles database connectivity• Querying done on model, not database• Translates queries into database queries• Tracks changes to objects in the model• Handles updates to the database
![Page 7: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/7.jpg)
Build a simple EDM
• Sample database, ProgrammingEFDB1• 2 tables, one view, some stored procedures• Create a new Console Application
In VS2010, File | New | ProjectSelect Console Application
![Page 8: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/8.jpg)
Name: Chapter2ConsoleAppLocation: (use default)Solution: ProgrammingEntityFramework
![Page 9: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/9.jpg)
Click OK
![Page 10: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/10.jpg)
•Right-click on Chapter2ConsoleApp in the Solution Explorer•Add | New Item
![Page 11: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/11.jpg)
Select ADO.NET Entity Data Model from the Templates list | Add
![Page 12: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/12.jpg)
Select Generate from database | Next
![Page 13: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/13.jpg)
Make sure your local SQL Server is activeSet up a new connection if necessaryChange the App.Config settings name to “Sample Entities” | Next
![Page 14: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/14.jpg)
Select Tables, Views | Change Model Namespace to SampleModel | Finish
![Page 15: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/15.jpg)
New model appears. Note Model1.edmx file. To display the model at any time, double-click on the Model1.edmx file in the Solution Explorer.
![Page 16: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/16.jpg)
THE DESIGNER WINDOW
![Page 17: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/17.jpg)
Gives a graphical view of the EDM and its members. Contact Entity, Address Entity, and vOfficeAddressEntity 2 from DB Tables, one from View connecting line One-to-Many relationship associations Scalar properties (ContactID, FirstName, etc) Navigation properties (Addresses, Contact) – Addresses property enables navigation from Contact Entity to set of Addresses related to that contact
![Page 18: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/18.jpg)
Using the Properties Window• A logical group of entities entity container• Click in the white space of the Designer Window, then look at
the Properties window.
You can modify the Entity Container Name and the Namespace You can set whether to pluralize new objects or not
![Page 19: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/19.jpg)
The Properties of an Entity• Click on the Contact Entity
Entity name: Contact db table Entity Set Name pluralized Entity names should be singular
Entity set a container for entities of a single type
![Page 20: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/20.jpg)
The Properties of an Entity Property
• Click on Contact’s FirstName property
FirstName is a non-nullable string with a maximum length of 50 characters, variable. It is not the Entity key.
The framework does not perform validation of the Max Length, Unicode, and Fixed Length properties. These properties are ignored.
![Page 21: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/21.jpg)
THE RAW FORMAT OF THE MODEL-- XML –
SUPPORTING METADATA
![Page 22: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/22.jpg)
XML Files
Entire Model
Entity Data Model Schema
Mappings
Database Objects Schema
*.CSDL
*.MSL
*.SSDL
*.EDMX
Design Time Run Time
Compile
![Page 23: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/23.jpg)
Supporting Metadata
• Sections– ConceptualModels– StorageModels (db schema)– Mappings
• Last 2 tell how to translate between conceptual model and actual data store
• Mapping – translates entities and properties (conceptual model) to tables and columns (StorageModel)
• Physical files stored in project assembly at compile time (you won’t see them).
![Page 24: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/24.jpg)
The Model Browser
• Right-click on the design surface• Choose Model Browser from the context menu.• Gives another view of the metadata.
![Page 25: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/25.jpg)
Raw XML
• Only a portion of the model is visible in the Designer.• View complete model in XML form.• To see the raw format of the model:
– In Solution Explorer– Right-click on Model1.edmx file– On context menu
• Select “Open With”• Choose XML Editor• Click OK• (Only one view of the model at a time is allowed – click OK to
close the Design View of the model when prompted.)
![Page 26: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/26.jpg)
Raw XML in the Designer
![Page 27: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/27.jpg)
Model with Sections Collapsed• To collapse the model
– Right-click on XML– Choose Outlining– Toggle All Outlining– Expand nodes to get the main sections of the model:
![Page 28: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/28.jpg)
CSDL: The Conceptual Schema
• If a line loses its hard returns, – Highlight the line– From VS Menu, select Edit | Advanced | Format
Selection to format the line• In ConceptualModels section, Click on + icons
to expose the Schema and EntityContainer
![Page 29: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/29.jpg)
Expanded Schema and Entity Container nodes
The above XML is the source of the graphical display we saw in the Designer.
![Page 30: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/30.jpg)
Model Elements We’ll Examine
• Entity Container• Entity Set• Entity Type• The Key Element• Property Elements• Associations• Navigation Property
![Page 31: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/31.jpg)
The Entity Container
• Name: – Defaults from model name– Change in model’s Property Window
• Wrapper for Entity Sets and Association Sets• Exposes Entity Sets– You write queries against these
![Page 32: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/32.jpg)
The Entity ContainerQUERY
Entity Container
ContactsAddresses
Contact Object
Contact Object
Contact Object
Address Object
Address Object
LazyLoading Annotation used as directions for generation of code
![Page 33: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/33.jpg)
The Entity Set• Gives access to individual entities when querying against
model• Example: “find some entities in the Contact EntitySet”
returns some Contact Entity Types• Entity Type: Strongly Typed Name for Entity
<EntitySet Name="Addresses" EntityType="SampleModel.Address" /> <EntitySet Name="Contacts" EntityType="SampleModel.Contact" /> <EntitySet Name="vOfficeAddresses" EntityType="SampleModel.vOfficeAddress" />
![Page 34: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/34.jpg)
The Entity Type• <EntityType Name="Address">• <Key>• <PropertyRef Name="addressID" />• </Key>• <Property Name="addressID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />• <Property Name="Street1" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />• <Property Name="Street2" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />• <Property Name="City" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />• <Property Name="StateProvince" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />• <Property Name="CountryRegion" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />• <Property Name="PostalCode" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />• <Property Name="AddressType" Type="String" Nullable="false" MaxLength="50" Unicode="true"
FixedLength="false" />• <Property Name="ContactID" Type="Int32" Nullable="false" />• <Property Name="ModifiedDate" Type="DateTime" Nullable="false" />• <NavigationProperty Name="Contact" Relationship="SampleModel.FK_Address_Contact" FromRole="Address"
ToRole="Contact" />• </EntityType>
Key element: Identity key for entity; may be compositetracks entity, helps perform updates and refreshes, very important
![Page 35: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/35.jpg)
Property Elements
• Can’t have XML and Designer open at the same time
• Close XML view, double-click .edmx file to view an element’s properties
• Not all propertes appear in XML , but do appear in Properties window– Currency mode, default value, Getter, Setter
• “Facets” are used to describe properties
![Page 36: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/36.jpg)
Association• Defines relationships
between entity types• Describes “end points” of
relationship and multiplicity (1, 0..1,*)
• Cascade – if Contact is deleted, delete associated Addresses
• Let’s look at the view in the Designer … Close the XML and double-click on the Model1.edmx in the Solution Explorer.
<Association Name="FK_Address_Contact"> <End Role="Contact" Type="SampleModel.Contact" Multiplicity="1"> <OnDelete Action="Cascade" /> </End> <End Role="Address" Type="SampleModel.Address"
Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Contact"> <PropertyRef Name="ContactID" /> </Principal> <Dependent Role="Address"> <PropertyRef Name="ContactID" /> </Dependent> </ReferentialConstraint>
![Page 37: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/37.jpg)
FK_Address_Contact Association• Defines relationships between
entities – Contact and Address• Focuses on “endpoints”• End1
– Contact– Assigned “role” – Contact– Also name that can be used in model to
point to this end– Multiplicity (only one Contact)– Navigation – other end of association– Cascade – delete addresses if contact is
deleted
• End2– Role – Address– Multiplicity – many addresses per contact– Association Set – may be more than one
per entity
![Page 38: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/38.jpg)
Referential Constraint
• For Foreign Keys• Shows dependency between related entities• Every Address must have a Contact• Used to maintain integrity of database
![Page 39: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/39.jpg)
Address_Contact Navigation Property
• Shows how to navigate to associated entity
• Association – how to navigate to associated entity (or entities)
• From Role, To Role – navigate from Address to Contact
• Multiplicity – returns 1 Contact object
![Page 40: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/40.jpg)
Contact.Addresses Navigation• This Navigation Property returns a
collection of addresses• No address for Contact, collection
will be empty• “Type” is a special EntityCollection,
not type implementing System.Collections.ICollection (not interchangeable)
![Page 41: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/41.jpg)
This CDSL
• Simple model• Conceptual Layer mirrors the database
schema• Customizing the model (later) gives real power
to Entity Framework
![Page 42: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/42.jpg)
SSDL (STORAGEMODEL SECTION)
![Page 43: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/43.jpg)
Expanded StorageModels Section
![Page 44: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/44.jpg)
StorageModels Section• Provides a description of the data store (database)• Tables called Type, Columns called Property• “Store” is in Namespace to minimize confusion• ProviderManifestToken – version of SQL Server
– You must modify manually if switching to SQL Server 2005• Entity container – derived from db name• Entity Type – name of table
– Property types – SQL Server data types– Identity column – StoreGeneratedPattern=“Identity” – identity value generated
by db when row is inserted, will not change– Other options
• “Computed” – values generated• “None” - default
![Page 45: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/45.jpg)
MSL: MAPPING FROM CONCEPTUAL MODEL TO STORAGE (AND BACK)
![Page 46: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/46.jpg)
MSL: Mappings Section
• Logically between concept and storage models• Maps entity properties back to tables and
columns of data store• Enables further customization of model• Use the Mapping Details Window– (Close XML view) Open Designer (dbl-clk on .edmx)– Right Click on Contact Entity– Select Table Mapping
![Page 47: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/47.jpg)
Mapping Details Window
• Maps Contact entity to Contact table• Maps to Contact Contact table in SSDL• Here, one to one relationship – can be customized• When you create mappings, wizard matches names for you• Note SQL data types
![Page 48: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/48.jpg)
Database Views: vOfficeAddress
• Note: DefiningQuery
![Page 49: Intro to Entity Framework](https://reader036.vdocuments.us/reader036/viewer/2022062521/56816785550346895ddc9c6e/html5/thumbnails/49.jpg)
Summary
• Introduced you to EDM and design tools• Created an EDM• Looked under the hood• Raw XML• Mappings• Enough to get started with Querying• Stay tuned for more exciting developments