ado.net 3.0 – entity data model

23

Upload: lesa

Post on 04-Feb-2016

42 views

Category:

Documents


0 download

DESCRIPTION

ADO.NET 3.0 – Entity Data Model. Gert E.R. Drapers Chief Software Architect Visual Studio Team Edition for Database Professionals Microsoft Corporation http://blogs.msdn.com/gertd. Agenda. ADO.NET Entity Framework Introduction Application Scenarios Direct Mapping - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ADO.NET 3.0 – Entity Data Model
Page 2: ADO.NET 3.0 – Entity Data Model

ADO.NET 3.0 – Entity Data Model

Gert E.R. DrapersChief Software Architect

Visual Studio Team Edition for Database Professionals

Microsoft Corporationhttp://blogs.msdn.com/gertd

Page 3: ADO.NET 3.0 – Entity Data Model

Agenda• ADO.NET Entity Framework Introduction• Application Scenarios

– Direct Mapping– Flexible Mapping Scenarios– Extensibility

• Usage Best Practices• Summary

Page 4: ADO.NET 3.0 – Entity Data Model

The Entity Framework

ADO.NET Providers

Dataset

ADO.NET 2.0

The Entity Data Model

Language Integrated Query (LINQ)

Bottle Winery

Supplier

<book> <title/> <author/> <year/> <price/></book>

ADO.NET Providers

Entity Client

Dataset

Object Services

LINQ over

Dataset

LINQ over

Entities

ADO.NET Entity Framework

Page 5: ADO.NET 3.0 – Entity Data Model

ADO.NET Data ProvidertADO.NET Data ProvidertADO.NET Data ProviderADO.NET Data Provider

Entity Framework Overview

ADO.NET Data ProviderADO.NET Data Provider

Object ServicesObject Services

LINQ to EntitiesLINQ to Entities

EDMMetaData

Store SchemaStore Schema

ConceptualSchema

ConceptualSchema

ObjectMetadata

ObjectMetadata

O-CMapO-CMap

C-SMapC-SMap

CQT

DataReader

DataReader

IEnumerable<T>ESQL

NativeSQL

CQT

ESQL

LINQ

EntityClient Data ProviderEntityClient Data Provider

Client View Engine CQT

Page 6: ADO.NET 3.0 – Entity Data Model

EDMGEN.EXE• Generates the model files• Model.ssdl (Store Schema Definition Language)

– Describes tables and columns which map to Entities and Relationships

• Model.csdl (Conceptual Schema Definition Language)– Describes the Entity Data Model (incl. EntitySets, EntityTypes,

Associations & AssociationsSets)• Model.msl (Mapping Specification Language)

– Describes how the Entity Framework maps between the Conceptual Model (CSDL) and the logical Storage Schema (SSDL)

• Model.cs or Model.vb– Partial class implementing the model created

Page 7: ADO.NET 3.0 – Entity Data Model

Direct Mapping• Scenario

– Database schema provides appropriate application model• Benefits

– Relationships– Common Extended Query Language– Common Queryable Schema– Common Metadata Services– Object Services

• Business Logic• Identity Management• Change Tracking

– Language Integrated Query (LINQ)

Page 8: ADO.NET 3.0 – Entity Data Model

Flexible Mapping• Scenario

– Database schema doesn't match application model• Benefits

– Richer Application Data Model

• Inheritance

• M:N Relationships

– Richer Mapping

• Inheritance options (TPH, TPT, TPC)

• Stored Procedures

• Entities split across tables

– Decouples application model from storage schema

• Multiple application models for same store

• Same application model over different storage schemas

• Independent evolution of application model/storage schema

Page 9: ADO.NET 3.0 – Entity Data Model

Extensibility• Scenario: Building a customizable framework• Benefits

– Flexible run-time mappings• i.e., Language-specific mappings

– Mapping to "Extensible Schemas"• EntityViews

– Vertical partitioning of framework and extensions• Separate mappings• Derived types

– Extending data classes

Page 10: ADO.NET 3.0 – Entity Data Model

Best Practices• Should I use Objects or DataRecords?• How long should I hold an ObjectContext?• Which mapping techniques should I use?• When should I used Stored Procedures or Table Valued

Functions?• How should I extend data classes?• When should I write my own data classes?• When should I use Custom Serialization?• Should I use LINQ, Query Builder Methods,

or eSQL?• Should I use LINQ to SQL or LINQ to Entities?

Page 11: ADO.NET 3.0 – Entity Data Model

Objects or DataRecords?• Objects (IEnumerable<T>)

– Strong typing– Business logic– Updating– Example: Interactive Windows application

• Data Records (IExtendedDataReader)– Performance for read-only streaming

• Sequential access• Avoid object construction overhead• Doesn't require generated classes

– Example: Directly serializing results to a Web response

Page 12: ADO.NET 3.0 – Entity Data Model

Holding ObjectContext• Web scenarios

– Typical pattern: Create, Use, dispose– ObjectContext holds resources

• Connections• Object References

– Use NoTracking for read-only scenarios• Windows Forms scenarios

– Hold as long as application interacts with results– ObjectContext mantains state

• Identity resolution• Change tracking

– New ObjectContext for each new query• Ensures data is fresh

Page 13: ADO.NET 3.0 – Entity Data Model

Mapping Techniques• Database views

– Common access patterns across applications• Optimization of access paths through Materialized Views

• Table/View mappings– Entity properties map directly to database columns

• Columns have same meaning throughout hierarchy• EntitySQL views in MSL

– Flexible mappings expressed in EntitySQL• Filter based on conditions, read-only properties, non-

equality conditions, computed properties, multiple extents mapped to same table

• Native SQL views in SSDL– Mapping on top of native SQL queries

• Store-specific queries• Expose as "EntitySet" to mapping layer

Page 14: ADO.NET 3.0 – Entity Data Model

Stored Procedures and TVFs• Stored Procedures

– Better control over operations on data within the database• Tighter control over operations, permissions• Provides level of indirection from storage schema

– Use for operations that affect state of the database• Inserts, updates, deletes• Required for updates against ESQL and Native SQL views

– Can return multiple results in one roundtrip• Tabled Valued Functions

– Better control over access to data within the database• Tighter control over operations, permissions• Provides level of indirection from storage schema

– Use for server-side functions that return data• Results of TVF are composable within a query

Page 15: ADO.NET 3.0 – Entity Data Model

Extending Data Classes• Derivation

– Adding new persistent fields to a base class• Derived types• Customization extensions

• Partial Classes– Business logic– Methods– Calculated values– Non-persistent fields– Custom serialization

• "Activity" Classes– Common operations on data classes

Page 16: ADO.NET 3.0 – Entity Data Model

Custom Data Classes• Entity Framework-generated data classes

– Derive from Entity Framework base class• Change Tracking, RelationshipManagement• Common Notifications, Property Validation

• Custom data classes– Derive from Entity Framework base class

• Generate common patterns • Insert common intermediate class

– Derive from your own base class• Implement IEntity/IEntityWithRelationships

– Expose EntityKey– Notify ObjectContext when data changes– RelationshipManager property to do relationship fixup

• Add attributes for persistent fields– How we get O/OC metadata today

– Investigating "Persistence Ignorance" scenarios

Page 17: ADO.NET 3.0 – Entity Data Model

Custom Serialization• Default serialization

– Serialize streams of Entities• Doesn't navigate relationships in order to avoid cycles

• Custom serialization– Serialize Graphs

• Manage relationship serialization to avoid cycles– Serialize to different shapes

• "Entity Projections"

Page 18: ADO.NET 3.0 – Entity Data Model

LINQ, Query Builder, or eSQL?• LINQ

– Strongly typed queries– Compile-time checking– Embedded in your application– LINQ-Mapped Canonical Functions

• ObjectQuery<T> builder methods– Queries built from user input– Iterative query building– Store functions

• eSQL– Dynamic/AdHoc queries– Persistent queries– Store functions

Page 19: ADO.NET 3.0 – Entity Data Model

LINQ to SQL or LINQ to Entities?• LINQ to SQL

– Emphasis on rapid application development– Direct mapping

• Single type hierarchy to single table/view/stored proc/TVF

• Simple renaming• Derive class for update logic/Stored Procedures

– Direct mapping to Microsoft SQL Server family of databases

– Microsoft Visual Studio 2008 RTM

Page 20: ADO.NET 3.0 – Entity Data Model

LINQ to SQL or LINQ to Entities?• LINQ to Entities

– Focus on enterprise-grade data scenarios– Flexible Mapping to Relational data

• Mapping a single class across multiple tables/views• Mapping to different types of inheritance• Directly modeling Many to Many relationships• Mapping to an arbitrary query against the store• Common model across products• Declarative stored procedures

– Access to Microsoft SQL Server family and other databases through ADO.NET provider model

– Microsoft Visual Studio 2008 update

Page 21: ADO.NET 3.0 – Entity Data Model

The LINQ ProjectC# 3.0C# 3.0C# 3.0C# 3.0 VB 9.0VB 9.0VB 9.0VB 9.0 OthersOthersOthersOthers

.NET Language Integrated Query.NET Language Integrated Query

LINQ LINQ toto

ObjeObjectscts

LINQ LINQ toto

ObjeObjectscts

LINQ LINQ toto

DataDataSetsSets

LINQ LINQ toto

DataDataSetsSets

LINQ LINQ toto

SQLSQL

LINQ LINQ toto

SQLSQL

LINQ LINQ toto

EntitEntitiesies

LINQ LINQ toto

EntitEntitiesies

LINQ LINQ toto

XMLXML

LINQ LINQ toto

XMLXML

ObjectsObjects

<book> <title/> <author/> <year/> <price/></book>

XMLXMLRelationalRelational

Page 22: ADO.NET 3.0 – Entity Data Model

Summary• Entity Data Model provides an application-oriented data

model– Entities and relationships

• Strong typing, polymorphism• Client Views provide flexible mapping to your storage

schema– Entities split between tables, Many:Many relationships,

mapping to store queries, etc.• Entity Framework lets you opt-in according to your

requirements– DataReaders/Objects– eSQL/QueryBuilder/LINQ– Generated/Custom Data Classes

Page 23: ADO.NET 3.0 – Entity Data Model

? Questions ?

http://blogs.msdn.com/gertd