140 likes | 307 Views
LINQ to Relational Data. Michael Pizzo Software Architect Data Programmability Microsoft Corporation. Agenda. LINQ to Relational LINQ to SQL LINQ to Entities LINQ to DataSet Summary. LINQ to SQL Strongly typed SQL Database. Design Points Rapid Development against SQL Database
E N D
LINQ to Relational Data Michael Pizzo Software Architect Data Programmability Microsoft Corporation
Agenda • LINQ to Relational • LINQ to SQL • LINQ to Entities • LINQ to DataSet • Summary
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 • RTM: Microsoft Visual Studio 2008 RTM
LINQ to SQLDirect Mapping • 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 SQLFeatures • 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
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 • Common Textual "EntitySQL" Language for Ad-Hoc queries • Targets: Microsoft SQL Server and third-party databases • RTM: Microsoft Visual Studio 2008 Update H1CY08
LINQ to EntitiesFlexible Mapping to Relational Data • Flexible Mapping • Mapping a single class to multiple tables/views • Mapping to different types of inheritance • Single Table per Class Hierarchy • Separate Table for each Class in a Hierarchy • Shared Table for Base Class members in a Hierarchy • Complex (composite) types • i.e., Street, City, Region, and Zip as "Address" • Directly mapping Many:Many relationships • Mapping to an arbitrary Query • Store Query • Expose arbitrary store query as a storage Table • Entity Query • Express mapping as EntitySQL against storage schema
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
LINQ to DataSetLINQ over Disconnected Cache with Change Tracking • Disconnected Cache • Offline/Remote • Data Aggregation • Application Data • All with Change Tracking • Queryable • Filter, Projection • Joins • Across Tables • Other in-Memory sources • Local expressions • All through Common LINQ syntax • RTM: Microsoft Visual Studio 2008 RTM
LINQ to DataSetTyped and UnTyped • UntypedDataSet • Call AsEnumerable() on DataTable • Reference Fields by Name • Use Field<T>(columnName) • Project out fields for strongly typed result • Typed DataSet • Use strongly typed accessors 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;
Summary • LINQ to SQL – Strongly Typed Database • Emphasis on rapid application development • Direct mapping to Microsoft SQL Server family of databases • Release in Microsoft Visual Studio 2008 RTM • LINQ to Entities – Flexible mapping to existing Schema • Focus on enterprise-grade data scenarios • Flexible Mapping to Microsoft SQL Server and third-party databases • Release in Microsoft Visual Studio 2008 update • CTPs on top of Microsoft Visual Studio 2008 Betas/RTM • 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 UntypedDataSet Support • Release in Microsoft Visual Studio 2008 RTM