José A. Blakeley Partner Architect Microsoft Corporation Michael Pizzo Principal Architect...
-
Upload
carlo-iverson -
Category
Documents
-
view
243 -
download
2
Transcript of José A. Blakeley Partner Architect Microsoft Corporation Michael Pizzo Principal Architect...
ADO.NET Entity Framework
José A. BlakeleyPartner ArchitectMicrosoft Corporation
Michael PizzoPrincipal ArchitectMicrosoft Corporation
10-12:00 Section 1: Evolution of ADO.NET12:00-1:15 <Lunch>1:15-2:30 Section 2: ADO.NET and LINQ2:30-2:45 <Break>2:45-4:00 Section 3: ADO.NET Entity Framework Deep Dive4:00-4:15 <Break>4:15-5:45 Section 4: Building on ADO.NET
ADO.NET A to Z: Agenda
Evolution of Data Access APIs Proprietary APIs Object Databases Standard SQL Data Access APIs
ODBC, DAO, RDO Componentized Data Access
OLE DB, ADO .NET Data Access
ADO.NET ADO.NET 1.0 Building a Data Platform The ADO.NET Entity Framework
Section 1: Evolution of ADO.NET
Application-specific, monolithic data access Files, hierarchical DBMSs CICS, IMS, etc.
Relational DBMSs emerged DBMS-specific APIs and protocols
OCI, DBLIB, SQL*, TDS, DRDA Embedded SQL
1xN data access problem Each app writes data-source-neutral data
access layer
Proprietary APIs – 70’s & early 80’s
ODBC Common C API for accessing SQL Databases Driver Manager loads DBMS-specific drivers Developed with SQL Access Group, ANSI/ISO
Adopted as SQL-CLI Addendum to SQL92 DAO
"OLE Automation" interface to Microsoft Access Provided Access, VB developers access to ODBC
sources RDO
DAO-style interfaces directly over ODBC Subsumed by DAO "ODBC Direct" mode
SQL APIs – Late 80’s & early 90’s
Transparent Language Persistence C++, Lisp, SmallTalk Transaction support - low throughput Very transparent persistence
Fast navigational performance Object graph loaded to memory Most navigation in memory Object faulting based on memory mapped IO and
pointer swizzling Limited query support
ODMG OQL standard No efficient implementations Limited indexing support
Limited customer acceptance
Object Databases – Mid 90’s
OLE DB COM-Based Data Access API Relational and non-relational sources
Tables, hierarchies, OLAP, documents Factored interfaces
Providers: SQL, ISAM, Excel, AS400 FS, Exchange Services: cursor engine, shaping, remoting, resource pooling,
transaction enlistment Used by SQL Server internally and externally
SQL Engine, Federated Query, Integration Services,… ADO
"OLE Automation" interface for OLE DB Interface for VB developers
Similar programming model to DAO Introduced disconnected RecordSets
Component Data Access – Late 90’s
Need a managed API for the new .NET Framework Consistency across framework
Explicit separation of connected access from disconnected Connected Model
Lower-level, common API for exposing the functionality native to a data store
Higher level abstractions, common functionality built on top Dataset as “in-memory data cache"
Database independent Predictable semantics, latency, error handling,… Combine data from multiple sources
Support for XML
ADO.NET 1.0 – Early 00’s
Evolution of Data Access APIs ADO.NET 1.0
Architecture Getting Data from a SQL Database Working with Data ADO.NET and XML
Building a Data Platform The ADO.NET Entity Framework
Section 1: Evolution of ADO.NET
Managed Provider
DataReader
CommandConnection
ADO.NET 1.0 Architecture
Controls,Designers,
Code-gen, etc
DataSet
DataAdapter
XmlReader
XmlWriter
OLTP operations,Programmatic Processing,
Frameworks
Getting Data From a SQL DatabaseADO.NET Data Provider Specific to a particular DBMS
Directly exposes consumer interfaces No more COM/Automation dichotomy
ADO.NET DataProvider Object Model Connection
Establishes connection to DataSource Transaction
Explicit Transaction Control Command
Execute SQL statement DataReader
Forward-only, Read-Only Result Stream Fields accessed through strongly typed, indexed accessors
Data store
DataProvider
Connection
CreateCommand()
ExecuteReader()
DataReader
Command ParametersParametersParameters
Relational View of Data Tables, Columns, Rows,
Constraints, Relations Directly create metadata
and insert data Explicit Disconnected Model
Disconnected, remotable object No knowledge of data source or properties
Common Behavior Predictable performance characteristics
Array-like indexing Strong Typing
Working with DataDataSet DataSet
TablesTable
ColumnsColumn
Constraints ConstraintRows
RowRelations
Relation
DataSet Scenarios Application Data Remoting Results
SOAP, WebMethods, Remoting Caching Results
ASP.NET Cache Persisting results
Save Data as XML, Schema as XSD User interaction
Scrolling, sorting, filtering DataView, DataViewManager
Binding Windows controls
DataAdapter
Loads a table from a data store and writes changes back. Exposes two methods:
Fill(DataSet,DataTable) Update(DataSet,DataTable)
Provides mappings between tables & columns
User provides insert/update/delete commands Allows use of Stored Procedures CommandBuilder component available
Allows single DataSet to be populated from multiple different datasources
Data store
DataAdapter
MappingsMappingsMappings
InsertCommand
UpdateCommand
DeleteCommand
SelectCommand
Fill() Update()
DataSet
ADO.NET and XML The DataSet
Loads/saves XML data into/out of DataSet Schema can be loaded/saved as XSD Schema can be inferred from XML Data
The DataSet can be associated with an XmlDataDocument Exposes a relational view over structured XML
According to the DataSet schema Allows strong typing, control binding, relational access of XML
data Allows XML tools (schema validation, XSL/T, XPath queries)
against relational data Preserves full fidelity of XML Document
ADO.NET 1.0
demo
Evolution of Data Access APIs ADO.NET 1.0 Building a Data Platform
Why a Conceptual Model? The Microsoft Entity Data Model Entity SQL
The ADO.NET Entity Framework
Section 1: Evolution of ADO.NET
Writing queries is difficult No help from compiler Results are untyped rectangular records
Database Schemas optimized for storage concerns Relational Tables contain flat, homogenous records
Implicit Logic Embedded in Application Brittle, Hard to maintain
Lack of common syntax across relational databases
The ProblemProgramming Data is Hard
Rapid Development Strongly typed queries Strongly typed results with Business Logic
Lower TCO Work with an explicit data model
Types, Inheritance, Relationships, Complex Properties,… Decouple application from storage schema
Better Portability Common query language across disparate sources
The OpportunityIncrease Developer Productivity
Applications Today… Implicitly Contain the Data
Model Logic and Model Intertwined Conceptual Mismatch Often encapsulate in a
"Data Access Layer"
Where’s Your Data Model?
Applications Today… Implicitly Contain the Data
Model Logic and Model Intertwined Conceptual Mismatch Often encapsulate in a
"Data Access Layer"
Where’s Your Data Model?
The Need… Applications work with a well
Defined Model Storage Schema Abstraction
Declarative mapping between application and storage models
No brittle, hard-coded mapping
An extended relational model with Entity-Relationship Model concepts Entity Types
Strong type with Identity Inheritance Scalar/Complex properties
EntitySets Hold instances of Entity Types
Similar to relational tables Can have multiple Entitysets of the same EntityTypes
Relationships ("Associations") Named relationships between Entities 0..1:*, 0..1:0..1, 1:1, 1:M, M:N Navigation may be exposed as NavigationProperties on
EntityTypes AssociationSets
Contains instances of associations May be queried directly
EntityContainers Contains EntitySets, AssociationSets
The Microsoft Entity Data Model
SalesPerson
EmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = …CarLicenseNum = ……
SalesPerson
EmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = …CarLicenseNum = ……
SalesPerson
EmployeeID = 729742LoginID = peteTitle = "Developer"VacationHours = 0…ExpenseAccount = true…
SalesPerson
EmployeeID = 294272LoginID = adamTitle = "Dev Lead"VacationHours = 0…
Reports
Manager
11
N
SQL extended to support the Entity Model SELECT Value
Promotes a single column to the record Entity construction
Useful in DefiningQueries Type Operations
OFTYPE, TREAT,… Relationship Navigation
Navigate(), . Manipulation of Keys
Key(), Ref()/DeRef(), CreateRef, Row() Other
Set()/Flatten() CrossApply, OuterApply Skip/Limit
EntitySQL
Data Access in the 80s ADO.NET 1.0 Building a Data Platform ADO.NET Entity Framework Introduction
Overview Entity Designer EntityClient Object Services
Section 1: Evolution of ADO.NET
ADO.NET Entity Framework
Data Access in the 80s ADO.NET 1.0 Building a Data Platform The ADO.NET Entity Framework
Overview Entity Designer EntityClient Object Services
Section 1: Evolution of ADO.NET
Define / Modify Conceptual Entity Model Define Entities, Associations, Functions Create from scratch or Reverse-Engineer from
database Define mapping to Relational Store
Generate/Refresh storage schema definition from Database
Generate CLR Objects for Entity Model Partial classes
Generate runtime metadata .csdl, .msl, .ssdl files or embed as resources
Entity Designer
No support for: "Model-First" Complex Types Abstract Types Multiple Entity Sets per Type
Practical limitation of ~120 Entities No design support for SSDL
Must manually edit XML Refresh overwrites manual changes to SSDL
Lack of Automatic Pluralization support Modify EntitySet names in model
Entity Designer V1 Limitations
Entity Designer
demo
Data Access in the 80s ADO.NET 1.0 Building a Data Platform The ADO.NET Entity Framework
Overview Entity Designer EntityClient Object Services
Section 1: Evolution of ADO.NET
…is an ADO.NET Data Provider EntityConnection
Metadata/Mapping specified in ConnectionString Can also load from XmlReader
EntityCommand Canonical EntitySQL syntax Stored Procedure Support Access to Provider-specific SQL
EntityDataReader Rectangular results described through IDataReader Typing, Polymorphism described through
IExtendedDataReader Limitations
No DML, DDL Call Functions in SSDL
No DataAdapter
EntityClient
EntityClient
demo
Data Access in the 80s ADO.NET 1.0 Building a Data Platform The ADO.NET Entity Framework
Overview Entity Designer EntityClient Object Services
Section 1: Evolution of ADO.NET
Object Services
Provides core object service abstractions Context, identity, state management Query, object materialization ObjectContext, ObjectQuery<T>, ObjectStateManager
Tools generate partial classes from EDM models Strongly typed collections Navigational patterns, relationship fix up Users can extend the partial classes to add business logic
Provides query and update interfaces LINQ integration, Entity SQL Strongly typed CRUD operations (add, delete, savechanges)
Object Services
demo
Questions?
Introduction to LINQLINQ to SQLLINQ to EntitiesLINQ to DataSet
Section 2: ADO.NET and LINQ
Queries as first-class concept in .NET languages
Builds on several language features Type inference, Delegates, Generics
Enabled by Lambda expressions Anonymous types Object initialization expressions Extension methods Query expressions
Introduction to LINQ
Introduction to LINQ
// Lambda Expressionsstring[] names = { "Luis", "Mary", "Mike", "Jose" };Display( names, s => s.Length > 3);
// Anonymous Types and object initializationvar emp = new { Name = "Mary", Company = "Microsoft",
Age = 30 };// Extension Methodspublic static class ExtensionMethods { public static void Display<T>(this T[] names,
Func<T, bool> filter) {foreach (T s in names) {
if (filter(s)) Console.WriteLine(s);}
}}// Query Expressionsvar query = from c in Customers
where c.Discount >= 3.0 && c.Discount < 4.0select new { c.Name, Perc = c.Discount / 100.0 };
Introduction to LINQ LINQ to SQL
Design Points Features Demo
LINQ to EntitiesLINQ to DataSet
Section 2: ADO.NET and LINQ
Direct Mapping Each class maps to a single SQL Schema Object
Table, View Stored Procedure, Table Valued Function
Simple renaming of Tables, Columns Foreign Keys can be expressed as Relationships
Properties to navigate in query, results Inheritance
Multiple Classes in a Hierarchy can map to a single Table/View/Stored Proc/TVF with a discriminator column
LINQ to SQLDirect Mapping
LINQ to SQLStrongly typed SQL Database
Design Points Rapid Development against SQL Database
Direct Mapping to SQL Server Schema Mappings expressed in Attributes or XML file
"Just Work" for common scenarios Execute when needed Naming Conventions
Business Logic Custom Insert/Update/Delete operations
Minimally Intrusive object model Provide Customization, Optimizations where
required Targets: Microsoft SQL Server
Customization Business Logic
Partial classes for generated Objects Add Methods, non-persistent members, etc. Business Logic through Partial methods based on naming
conventions Update Logic
Implement partial methods in derived Class Call Stored Procedures or invoke custom logic
Optimizations Loading Options
"Span" related information ObjectTrackingEnabled DeferredLoadingEnabled
Compiled Query Save overhead of SQL generation from Language Expression
LinqDataSource for use in ASP.NET
LINQ to SQLFeatures
LINQ to SQL
demo
Introduction to LINQLINQ to SQL LINQ to Entities
Design Points Features Demo
LINQ to DataSet
Section 2: ADO.NET and LINQ
LINQ to EntitiesFlexible Mapping to Relational Data
• Design Points• Flexible Mapping to Existing Relational Schema
• Well defined Conceptual model• Share common model across products (Reporting,
Analysis, etc…)• Declarative Mapping between Application and Store• Allows Storage Schema and Application to evolve
independently• Explicit Operations
• Server interactions should be explicit• Build implicit logic on top of explicit operations
Targets: Microsoft SQL Server and third-party databases
LINQ to EntitiesFeatures
Customization Business Logic
Partial Classes, Events, Partial Methods Update Logic
Generated Update Views Declarative stored procedures
Optimizations "Span" related members NoTracking
Extensibility Partitioning of Metadata Flexible Runtime Mapping Metadata Pluggability
EntityDataSource for use from ASP.NET
LINQ to Entities
demo
Introduction to LINQLINQ to SQLLINQ to Entities LINQ to DataSet
Section 2: ADO.NET and LINQ
LINQ to DataSetLINQ over Disconnected Cache with Change Tracking
Disconnected Cache Offline/Remote Data Aggregation Application DataAll with Change Tracking
Queryable Filter, Projection Joins
Across Tables Other in-Memory sources
Local expressionsAll through Common LINQ syntax
LINQ to DataSetTyped and UnTyped
Untyped DataSet Call AsEnumerable() on DataTable Reference Fields by Name
Use Field<T>(columnName) Project out fields for strongly typed result
var query = from row in myDataSet.Tables["Customers"].AsEnumerable() where row .Field<string>("City") == "London" select new { row.Field <string> ("CustomerID"),
row.Field <string> ("ContactName") } ;
var query = from customer in northwind.Customers where customer.City == "London" select customer;
Typed DataSet Use strongly typed accessors
LINQ to DataSet
demo
LINQ to SQL Strongly Typed Database Emphasis on rapid application development Direct mapping to Microsoft SQL Server family of databases
LINQ to Entities Flexible mapping to existing Schema Focus on enterprise-grade data scenarios Flexible Mapping to Microsoft SQL Server and third-party
databases
LINQ to DataSet In-Memory Cache w/Change Tracking All the scenarios where DataSet is useful today
Offline, Disconnected, Aggregation Change Tracking..Plus support for Query Operations
Strongly typed or Untyped DataSet Support
Summary: ADO.NET and LINQ
Questions?
Customizing Data Classes Entity Framework Mapping Scenarios Database Design Considerations Advanced Mapping Techniques
Section 3: ADO.NET Entity Framework Deep Dive
Partial Classes Add business logic, methods, non-persisted
properties to partial classes Codegen Customization
Add custom logic to Property, Type Generation Attributes, Interfaces, Members, BaseClass,… Getters, Setters, BackingField, Return Type,…
Custom Classes Write your own classes that
Derive from EntityObject, or Implement interfaces:
IEntityWithKey, IEntityWithChangeTracker, IEntityWithRelationships
Customizing Data Classes
Customizing Data Classes
demo
Customizing Data Classes Entity Framework Mapping Scenarios
Core Mapping Scenarios Function Mapping Mapping Limitations
Database Design Considerations Advanced Mapping Techniques
Section 3: Entity Framework Deep Dive
EntitySets map to Table(s) EntityTypes, Properties can be renamed A single EntitySet may be mapped to multiple tables Different EntityTypes in a hierarchy can be mapped to:
Different columns within a table or different tables An EntitySet may be mapped to a subset of the rows
within a table AssociationSets map to tables with foreign keys
M:N relationships map to join table 0..1 : * relationships may be "co-located" Relationships may map to/from subtypes in a hierarchy Can have "Owning" relationships
Core Mapping Scenarios
Functions exposed through Function Imports Parameters may be mapped Stored Procedures can return different results
Mapping CUD operations to Functions Insert, Update, Delete operations
Function Mapping
Must use a common EntityKey throughout type hierarchy
Relationships must be based on EntityKey EntityKey can't contain reference
Function results must be mapped to EntityTypes Can't Map multiple entities to different columns
of a single row If used, modification functions must include all
CUD operations on EntitySet and References Can't map Multiple Results from a Stored Proc No Support for Composable TVFs in V1
Core Mapping Limitations
Use a common PK… For all tables in a Hierarchy When splitting information across tables For Relationships
Hierarchy Models Single Table per Hierarchy generally results in
simpler queries (fewer joins) Separate table for each subtype when most
queries are against a single Subtype Separate table for additional subtype columns
When most queries are against supertype
Database Design Considerations
Customizing Data Classes Entity Framework Mapping Scenarios Database Design Considerations Advanced Mapping Techniques
Anatomy of an .edmx file Complex Types Custom Mapping
Section 3: Entity Framework Deep Dive
Conceptual Schema Definition (CSDL) Container
EntitySets AssociationSets
Entity Types Association Types
Storage Schema Definition (SSDL) Same Structure as CSDL
Mapping Definition (MSDL) Mapping for EntitySets, AssociationSets
Anatomy of an .edmx File
Conceptual Schema Definition (CSDL)<Schema Namespace="AdventureWorksModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm"> <EntityContainer Name="AdventureWorksEntities"> <EntitySet Name="Contacts"
EntityType="AdventureWorksModel.Contact" /> <AssociationSet Name="ManagerEmployees"Association="AdventureWorksModel.ManagerEmployee"> <End Role="Employees" EntitySet="Contacts" /> <End Role="Manager" EntitySet="Contacts" /> </AssociationSet></EntityContainer> <EntityType Name="Contact"> <Key> <PropertyRef Name="ContactID" /> </Key> <Property Name="ContactID" Type="Int32" Nullable="false" /> <Property Name="Title" Type="String" /> <Property Name="FirstName" Type="String" Nullable="false" /> <Property Name="LastName" Type="String" Nullable="false" /></EntityType> <Association Name="ManagerEmployee"> <End Role="Employees" Type="AdventureWorksModel.Employee" Multiplicity="*" /> <End Role="Manager" Type="AdventureWorksModel.Employee" Multiplicity="0..1" /></Association> </Schema>
Storage Schema Definition (SSDL)<Schema Namespace="AdventureWorksModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl"> <EntityContainer Name="HumanResources"> <EntitySet Name="Contact"
EntityType="AdventureWorksModel.Store.Contact" Schema="Person" /> <AssociationSet Name="FK_Employee_Employee_ContactID"
Association= "AdventureWorksModel.Store.FK_Employee_Employee_ContactID"> <End Role="Employees" EntitySet="Employee" /> <End Role="Manager" EntitySet="Employee" /> </AssociationSet> </EntityContainer> <EntityType Name="Contact"> <Key> <PropertyRef Name="ContactID" /> </Key> <Property Name="ContactID" Type="int" Nullable="false"
StoreGeneratedPattern="Identity" /> <Property Name="Title" Type="nvarchar" /> <Property Name="FirstName" Type="nvarchar" Nullable="false" /> <Property Name="LastName" Type="nvarchar" Nullable="false" /> </EntityType> <Association Name="FK_Employee_Employee_ContactID"> <End Role="Employees" Type="AdventureWorksModel.Store.Employee" Multiplicity="*" /> <End Role="Manager" Type="AdventureWorksModel.Store.Employee" Multiplicity="0..1" /></Association>
</Schema>
Mapping Definition (MSL)<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS"> <EntityContainerMapping StorageEntityContainer="HumanResources"
CdmEntityContainer="AdventureWorksEntities">
<EntitySetMapping Name="Contacts" TypeName="AdventureWorksModel.Contact" StoreEntitySet="Contact">
<ScalarProperty Name="ContactID" ColumnName="ContactID" /> <ScalarProperty Name="Title" ColumnName="Title" /> <ScalarProperty Name="FirstName" ColumnName="FirstName" /> <ScalarProperty Name="MiddleName" ColumnName="MiddleName" /> <ScalarProperty Name="LastName" ColumnName="LastName" /> </EntitySetMapping>
<AssociationSetMapping Name="ManagerEmployees"
TypeName="AdventureWorksModel.ManagerEmployee" StoreEntitySet="Employee"> <EndProperty Name="Employees"> <ScalarProperty Name="ContactID" ColumnName="ContactID" /> </EndProperty> <EndProperty Name="Manager"> <ScalarProperty Name="ContactID" ColumnName="ManagerID" /> </EndProperty> <Condition ColumnName="ManagerID" IsNull="false" /> </AssociationSetMapping> </EntityContainerMapping></Mapping>
Customizing Data Classes Entity Framework Mapping Scenarios Database Design Considerations Advanced Mapping Techniques
Anatomy of an .edmx file Complex Types Custom Mapping
Section 3: Entity Framework Deep Dive
Complex Types Define your Complex Type in CSDL<ComplexType Name ="FullName"> <Property Name="Title" Type="String" /> <Property Name="FirstName" Type="String" Nullable="false" /> <Property Name="MiddleName" Type="String" /> <Property Name="LastName" Type="String" Nullable="false" /></ComplexType>
<EntityType Name="Contact"> <Key> <PropertyRef Name="ContactID" /> </Key> <Property Name="ContactID" Type="Int32" Nullable="false" /> <Property Name="Name" Type="Self.FullName" Nullable="false"/><!-- … --></EntityType>
Use your ComplexType in your Entities
Map the Complex Type in your MSL<EntitySetMapping Name="Contacts" TypeName="AdventureWorksModel.Contact"
StoreEntitySet="Contact"> <ScalarProperty Name="ContactID" ColumnName="ContactID" /> <ComplexProperty Name="Name"> <ScalarProperty Name="Title" ColumnName="Title" /> <ScalarProperty Name="FirstName" ColumnName="FirstName" /> <ScalarProperty Name="LastName" ColumnName="LastName" /></ComplexProperty></EntitySetMapping>
Entity Framework Mapping Scenarios Database Design Considerations Customizing Data Classes Advanced Mapping Techniques
Anatomy of an .edmx file Complex Types Custom Mapping
Section 3: Entity Framework Deep Dive
Multiple EntitySets of the same EntityTypeSpecify the same EntityType in multiple EntitySet
definitions Useful for partitioned data
Split Model Across FilesReference other CSDL, MSL using <Using/> Common SSDL across Referenced MSL files Useful for:
Dealing with large models Partitioning model between users
Customize Conceptual Definition
Specify Custom Mapping to exposed tablesSpecify the EntitySQL that describes the EntitySet
mapping using <QueryView/> Use ModificationFunctionMappings to update Useful for:
Getting around mapping limitations Different Keys in Hierarchy Relationships on non-Key fields Splitting an Entity between tables with different
keys
Customize Mapping Definition
In-Line Function Definitions:Expose arbitrary command as a function using
<CommandText/> Useful for:
Defining client side functions Defining ModificationFunctionMappings
Change the columns used as Keys Useful for hierarchies without a common key
Change the underlying table:Specify table using the store:Table attribute Useful for exposing the same table as multiple
targets in SSDL
Customize the Storage View
In-Line Table DefinitionsExpose a native SQL command as a virtual table
using <DefiningQuery/> Use ModificationFunctionMappings to update Useful for:
Ultimate escape hatch for mapping to storage constructs
Utilizing provider-specific commands Composing parameterless TVFs Adding query hints Provider-specific datatypes
Tip: After modifying SSDL, don’t refresh from Database; instead add to new model and Copy/Paste
Customize the Storage View
Custom Mapping
demo
Questions?
Data Access Across Tiers Entity Framework Remoting Entity DataSourceControl ADO.NET Data Services
ADO.NET and SQL ServerFuturesSummary: ADO.NET A to Z
Section 4: Building on ADO.NET
Generated Entities are DataContract Serializable Graph Serialization for current values
Track Changes on Client For Optimistic Updates Send original and current For “Last Writer Wins” just send current Use EntityReference to set related Key Values
Apply changes on server Inserts:
Call ObjectContext.AddObject() Updates:
Attach (or query for) Original Entity Call ObjectContext.ApplyPropertyChanges()
Deletes: Attach (or query for) Original Entity Call ObjectContext.DeleteObject()
Call SaveChanges() when done
Entity Framework Remoting
Entity Framework Remoting
demo
Create an ASP.NET Web Application Define or reference your Entity Data Model Add an EntityDataSource Control Configure the EntityDataSource Control
Set NamedConnection and DefaultContainer Set EntitySetName, select properties to display Specify Filter, Ordering, Grouping, Span, TypeFilter
Include Parameters from: Controls, Cookies, QueryString, Form, Session
Specify support for Insert, Update, Delete Bind DataSourceControl to grid, dropdown,…
EntityDataSource Control
Entity Data Source Control
demo
ADO.NET Data Service Publish your Data Model as a REST-based Web Service Supports pluggable DataServiceProviders
EntityFramework, LINQ , Custom DataServiceProviders Protocol
Model-based Contract Uniform Interface
REST-style URI-based addressing CRUD using standard HTTP verbs
Format Independence: Results as ATOM, JSON Shared with SQL Server Data Services, LIVE, …
Extensibility Query Interceptors Service Operations
ADO.NET Data ServicesData Services for the Web
Consuming Data Services Any HTTP client
From Any Browser Specify URLs for query, insert, update, delete
LINQ Enabled Silverlight & .NET Framework Client Library Build queries using LINQ Results as generated or hand-coded objects Save changes back to Service
Future ASP.NET Data Source Control AJAX Client Library (CodePlex)
ADO.NET Data ServicesData Services for the Web
ADO.NET Data Services
demo
Data Access Across Tiers ADO.NET and SQL Server
New data Types Table Valued Parameters New Date Types Spatial: Geography, Geometry Hierarchy Id
Asynchronous Operations MultipleActiveResultSets SqlBulkCopy SqlNotifications SQL Server Functions callable from EF
Futures
Section 4: Building on ADO.NET
New SQL Server 2008 Data Types
Table-valued Parameters Date Types
Date, Time, DateTimeOfsset, DateTime2 Spatial: Geometry and Geography HierarchyId Sparse Columns File Stream
Input parameters of Table type on SPs/Functions
Optimized to scale and perform better for large data
Behaves like BCP in serverSimple programming modelStrongly typedReduce client/server round trips Do not cause a statement to recompile
Table-valued Parameters (TVP)
CREATE TYPE myTableType AS TABLE (id INT, name NVARCHAR(100),qty INT);
CREATE PROCEDURE myProc (@tvp myTableType READONLY) AS UPDATE Inventory SET
qty += s.qty FROM Inventory AS i INNER JOIN @tvp AS tvp ON i.id = tvp.idGO
Table-valued Parameters (TVP) TVP Client Stack Support Fully supported in ADO.Net 3
New Parameter type: SqlDbType.Structured Parameters can be passed in multiple ways
DataTable IEnumerable<SqlDataRecord> (fully streamed) DbDataReader
Table-valued Parameters ADO.NET Example using DataTableUsing (MyConnection){ //Create a data table DataTable dt = new DataTable(“TVPOrdersDataTable”); dt.Columns.Add(“ProductType”, typeof(string)); dt.Columns.Add(“Quantity”, typeof(int)); // Add rows dt.Rows.Add(“Canon Digital Camera”, 20); dt.Rows.Add(“June”, 10); dt.Rows.Add(“Xbox-360”, 8);
// Create a command and bind parameter SqlCommand tvp_cmd = new SqlCommand(“sp_UpdataInventory”,
MyConnection); SqlParameter tvpParam = tvp_cmd.Parameters.AddWithValue(
@OrdersTvp, dt); //Execute command tvp_cmd.ExecuteNonQuery();
New SQL Server 2008 Date Types
New DateTimeOffset
SqlCommand command = new SqlCommand(string.Empty, sqlConnection);
command.CommandText = "insert into MoviesRented values(@customerId, @MovieID, @RentalDate,
@DueDate)";….// create a parameter for RentalDateSqlParameter rentDateParam = new SqlParameter("RentDate", System.Data.SqlDbType.DateTimeOffset); rentDateParam.Value = DateTimeOffset.Now; command.Parameters.Add(rentDateParam);
// create a parameter for DueDateSqlParameter dueDateParam = new SqlParameter("DueDate", System.Data.SqlDbType.DateTimeOffset); dueDateParam.Value = DateTimeOffset.Now.AddDays(7); command.Parameters.Add(dueDateParam);….
New DateTimeOffset
// create a command to get the DueDate SqlCommand command =
new SqlCommand(String.Empty, sqlConnection); command.CommandText = "select DueDate from MoviesRented where MovieId = @MovieId";… // Execute the DataReader // using (SqlDataReader dataReader = command.ExecuteReader()) { if (dataReader.Read() == false) {
Console.WriteLine("Movie has not been rented");
}DateTimeOffset dueDate =
dataReader.GetDateTimeOffset (0); Console.WriteLine("Movie due back on : {0}", dueDate); }
Asynchronous Operations
SqlClient provides true async I/O Opening connections Executing commands
Client applications don’t block UI Server applications execute multiple requests without
blocking threads Consistent with .NET async pattern
BeginXXX and EndXXX methods Use IAsyncResult to poll or block …Or use a callback
Callback is called in a different thread
Asynchronous Example
// Poll for completionIAsyncResult result = cmd.BeginExecuteReader();while(!result.IsCompleted) {
// do some work}SqlDataReader reader = cmd.EndExecuteReader(result);
// Use a CallbackIAsyncResult result = cmd.ExecuteReader(
new AsyncCallback( myDataCallback ));
// do other work…
// optionally wait using sync objectresult.WaitHandle.WaitOne();
public void myDataCallback( IAsyncResult result ) {SqlDataReader reader = cmd.EndExecuteReader(result);
}
Multiple Active ResultSets
Connection String attribute:MultipleActiveResultSets=True
Enables multiple results per connection Performance and scalability gain
Multiple sessions per physical connection Reduction in client and server resources usage Avoids pooling/connection initialization logic Combine with async for maximum perf
Single result per command Available against SQL 2005 and newer
Deliver a message to a queue when a change occurs that would affect results Rows in the result changed/deleted New rows added that would have been in the result
Client specifies message body and delivery queue Any authorized client can check for notifications
Poll or blocking query Use SqlDependency for ease of use Use NotificationContext to automatically enlist
any commands w/in context Enables notification w/LINQ to SQL and LINQ to Entities
Notifications
Notification Example
public SqlDataReader GetProducts(int Category) {SqlCommand cmd = new SqlCommand(
"Select ProductName, UnitPrice from Products " +"where CategoryID = @CatID", cnn);
cmd.Parameters.Add("@CatID",Category);cmd.Notification = new SqlNotificationRequest(
Category.ToString(), // message"myQueue", // message body3000); // timeout
return cmd.Execute();}
public void WaitForChanges() {SqlCommand cmd = new SqlCommand(
"Receive message_body from myQueue " +"WITH wait_for_results", cnn);
cmd.CommandTimeout = 0;int category = (int)cmd.ExecuteScalar();Console.WriteLine("Category {0} changed.",category);
}
Bulk-Copy
High-perf bulk load Load Data from several data sources
IDataReader, DataTable, DataRow[] No support for BCP files and BCP out
public void LoadFromDataReader(IDataReader reader){
// Copy the Data to SqlServerSqlBulkCopy bcp =
new SqlBulkCopy( connectString );bcp.DestinationTableName = "Customers";bcp.WriteToServer( reader );
}
User-Defined Types
SqlClient natively supports UDTs UDTs are CLR types
Data readers return instances of UDTs Parameters can be instances of UDTs
SQLOLEDB also provides UDT support Using COM/interop
Assembly location Application can use strong typed references to local assembly
Methods execute on client Assembly downloaded if needed
Must be granted download priviledge on assembly Security warning on download Class is limited by security policy, application
domain permissions Access objects through reflection
UDT Example
SqlConnection cnn = new SqlConnection(connectString);cnn.Open();SqlCommand cmd =
new SqlCommand("SELECT p FROM PointTable", cnn );SqlDataReader reader = cmd.ExecuteReader();while( reader.Read() ) {
Point point=(Point)reader[0];Console.WriteLine(
"x:{0}, y:{1}", point.x, point.y );}cnn.Close();
SQL Server Functions in EntiySQLAggregatesAVGCHECKSUM_AGGCOUNTCOUNT_BIGMAXMINSTDEVSTDEVPVARVARP
String FunctionsASCIICHARCHARINDEXDIFFERENCELEFTLENLOWERLTRIMncharPATINDEXQUOTENAMEREPLACEREPLICATEREVERSERIGHTRTRIMSOUNDEXSPACESTRSTUFFSUBSTRINGUNICODEUPPER
Math FunctionsABSACOSASINATANATN2CEILINGCOSCOTDEGREESEXPFLOORLOGLOG10PIPOWERRADIANSRANDROUNDSIGNSINSQRTSQUARETAN
Date FunctionsDATEADDDATEDIFFDATENAMEDATEPARTDAYGETDATESYSDATETIMESYSUTCDATETIMESYSDATETIMEOFFSETGETUTCDATEMONTHYEAR
System FunctionsDATALENGTHCHECKSUMNEWIDCURRENT_TIMESTAMPCURRENT_USERHOST_NAMEUSER_NAMEISNUMERICISDATE
ADO.NET and SQL Server
demo
Data Access Across TiersADO.NET and SQL Server Futures
Section 4: Building on ADO.NET
ADO.NET Entity Framework Futures
Tool Support Model-First Support for Larger Models Complex Types Exensibility
Data Class Support Pure POCO Templates
Improved Mapping Support Composable TVFs
New Entity Services Synchronization, Offline SSRS integration
DataSet Queryable, remoteable In-memory relational collection
DBMS-Specific ADO.NET Data Provider Direct access to native store syntax, types
EntityClient Canonical Ad hoc queries with Flexible Mapping
LINQ to SQL Strongly typed Microsoft SQL Server schema LinqDataSource for use in ASP.NET
Summary: ADO.NET Data Access Components
ObjectServices Strongly typed Entity Model with Flexible Mapping LINQ to Entities
Strongly typed Queries EntitySQL
Complex Queries, Ad-hoc Queries, Provider-specific Functions EntityDatasource
Simple ASP.NET Integration ADO.NET Data Services
Standard, Robust, REST-based service Easy to use, Highly Extensible .NET Client for simple programming
SummaryADO.NET Data Access Components
ADO.NET Entity Framework V1 delivers… Applications work in terms of a rich Conceptual Model
Strongly typed LINQ queries Ad-hoc ESQL queries Untyped EntityClient
Flexible mapping to existing storage schemas, DBMSs Tool support for generating model, mapping
ADO.NET Entity Framework promises… More Mapping Flexibility Richer Entity Model Concepts Less Intrusive Persistence Logic Better support for provider-specific functions Better tooling for model/code-driven development More services: Synchronization, Data Definition,…
Summary: ADO.NET Entity Framework
ADO.NET Forum:http://blogs.msdn.com/adonet/default.aspx
LINQ to SQL Forumhttp://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=2035&SiteID=1 ADO.NET Entity Framework Forum:http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=533&SiteID=1
ADO.NET Entity Framework Design BLOG:http://blogs.msdn.com/efdesign/
Danny Simmons’ BLOG:http://blogs.msdn.com/dsimmons/default.aspx
Summary:For More Information…
Questions?
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market
conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.