230 likes | 309 Views
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
E N D
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 • Flexible Mapping Scenarios • Extensibility • Usage Best Practices • Summary
The Entity Framework The Entity Data Model ADO.NET Entity Framework LINQ over Entities ADO.NET 2.0 Object Services LINQ over Dataset Dataset ADO.NET Providers Entity Client Language Integrated Query (LINQ) Dataset <book> <title/> <author/> <year/> <price/> </book> ADO.NET Providers
CQT Entity Framework Overview EDM MetaData LINQ ESQL Native SQL ESQL CQT CQT LINQ to Entities Object Metadata Object Services O-C Map EntityClient Data Provider Client View Engine Conceptual Schema C-S Map DataReader DataReader IEnumerable<T> ADO.NET Data Provider Store Schema ADO.NET Data Provider ADO.NET Data Providert
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
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)
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
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
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?
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
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
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
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
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
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
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"
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
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
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
<book> <title/> <author/> <year/> <price/> </book> Relational Objects XML The LINQ Project .NET Language Integrated Query C# 3.0 VB 9.0 Others LINQ toObjects LINQ toDataSets LINQ toSQL LINQ toEntities LINQ toXML
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
? Questions ? http://blogs.msdn.com/gertd