deep dive into the ado.net entity framework. agenda entity data model advanced mapping advanced...

of 18 /18
Deep Dive into the ADO.NET Entity Framework

Author: myrtle-crawford

Post on 16-Jan-2016

239 views

Category:

Documents


4 download

Embed Size (px)

TRANSCRIPT

DPE Presentation Template

Deep Dive into the ADO.NET Entity Framework

Abstract:

Duration: 1 hour

Level: 300-4001AgendaEntity Data ModelAdvanced Mapping

Advanced queryingEntity SQLObject Services

ADO.NET Metadata

Estimated Time: 2 minutes

Talking Points: In this session were going to revisit both the Entity Data Model and the Entity Framework, and dive deeper into some of their respective advanced features. This will include some advanced mapping scenarios, as well as some more in depth querying options. Well also introduce a feature called ADO.NET Metadata.2Advanced MappingComplex TypesTable-Per-Hierarchy (TPH)Table-Per-Type (TPT)Query ViewsDefining QueriesEstimated Time: 2 minute

Talking Points: The Entity Data Model accommodates a wide array of mapping scenarios that allow you to shape your application model exactly the way you want it, regardless of the shape of your underlying database. The EDM allows three other mapping scenarios that well talk about in depth: Complex Types Table-Per-Hierarchy Table-Per-TypeQuery Views Defining Queries3Complex TypesCustomerFirstNameLastNameAddressAddressAddressLine1AddressLine2City StateZipCustomerAddressLine1AddressLine2City StateZipFirstNameLastNameEstimated Time: 3 minutes

Talking Points: Imagine you have a table in your database that looks like this. A standard Customer table that includes name and address information. You could easily achieve a one-to-one mapping to this table with your EDM and have a corresponding Customer class that has properties representing each of the columns in the Customer table. But in order to add more semantic value to your model, you prefer to map like this [Advance Animation] Here you see we mapped a Customer class that contains the name information, but then we created a nested type called Address that houses the address information. This nested type is called a Complex Type. It allows you to map a series of columns in a table as a separate type.4Table Per HierarchyPersonFirstNameLastNameCustomerAddressLine1CustomerAddressLine1AddressLine2City StateZipFirstNameLastNameAddressLine2City StateZipCustomerTypeDiscriminatorEstimated Time: 3 minutes

Talking Points: Here we have the same Customer table in our database as shown in the last example. In this case, instead of creating a separate type for address information, we want to create a hierarchy of classes that we can map to, each of which will contain different sets of columns from the actual table [Advance Animation] Here you see that weve created a new Person class that contains just the name information. Then we created a Customer class that inherits from Person and then adds the address information. Because Customer inherits from Person, it will inherit the name information as well. This scenario allows you to partition a single table into a hierarchy of classes. This is a very simple example, but you can imagine that we could have a lot longer inheritance tree.5Table Per TypePersonFirstNameLastNameCustomerAddressLine1PersonFirstNameLastNameAddressLine2City StateZipAddressLine1AddressLine2City StateZipCustomerPersonIDPersonIDEstimated Time: 3 minutes

Talking Points: Now weve got two tables in our database: Person and Customer. Person contains the name information, and the Customer table has a foreign key lookup to the person, and adds address information. We actually like the way these tables are shaped in the database and would like to mimic them in our conceptual model[Advance Animation] Here you see we have the same set of classes we had in the previous scenario, but instead of partitioning a single table into a hierarchy of classes, were mapping a hierarchy of tables into a hierarchy of classes.

6Query ViewsCustomerAddressCity FirstNameLastName

Entity SQLEstimated Time: 3 minutes

Talking Points: Here we have a query written in Entity SQL that pulls a few columns from a couple different entities. Instead of mapping directly to the underlying entities wed like to just map directly to the shape of this query[Advance Animation] Here weve created a class that maps to the columns projected from a query instead of actual tables. This allows a level of flexibility that is sometimes needed. This query would be defined in the MSL layer of the EDM and is generic to the underlying database.7Defining QueriesCustomerAddressCity FirstNameLastName

T-SQLEstimated Time: 3 minutes

Talking Points: Here we have a database query written in T-SQL that pulls a few columns from a couple different tables, and performs a few joins. Instead of mapping directly to the underlying tables wed like to just map directly to the shape of this query[Advance Animation] Here weve created a class that maps to the columns projected from a query instead of actual tables. This allows a level of flexibility that is sometimes needed. This query would be defined in the SSDL layer of the EDM and is database-specific. When you add a view to your EDM using the designer, it is actually mapping them using Defining Queries.8DemoAdvanced MappingEstimated Time: 10 minutes

Description:This demo is meant to show a simple example of all the advanced mapping scenarios: Map a complex type using the XML view of the model and show it reflected in the designer. Map a simple TPH scenario using the designer. Map a simple TPT scenario using the designer.9Entity SQLFunctionsCanonicalStore-specificPagingTOPSKIP/LIMITGroupingEstimated Time: 5 minutes

Talking Points: In addition to the standard Entity SQL keywords, you can also leverage two different types of functions: Canonical Store-specific Canonical functions are a set of Entity SQL functions that provide common tasks you might need to perform, without coupling you to a specific SQL flavor (i.e. T-SQL, PL/SQL). They include functions like IndexOf, GetDate, NewGuid, Round. The actual function used will be translated into the specific database-specific function by the underlying ADO.NET provider. Store-specific functions allow you to call a function that is specific to the SQL flavor of your underlying provider. This allows you to leverage functionality that your underlying database server provides that isnt included in the Entity SQL canonical function-set. In order to use store-specific functions you have to import the respective namespace by adding a using statement to your Entity SQL query (i.e. using SqlServer). The canonical functions are part of the Edm namespace which is the default namespace which is why you dont have to import it. Entity SQL provides rich paging support in the form of TOP and SKIP/LIMIT. You append the TOP function to your SELECT clause, and specify how many records you want to return. In addition you can append the ALL or DISTINCT modifiers to your TOP statement to specify whether you only want unique records returned. You append the SKIP/LIMIT pair to your ORDER BY clause, specifying how many records to skip and then return. You can only use SKIP/LIMIT if you sort your query. Entity SQL also provides GROUP BY and HAVING clauses that work like youre familiar with them in T-SQL.10Canonical FunctionsAggregateBitwiseDate/TimeMathStringDemoEntity SQLEstimated Time: 10 minutes

Description:This is a simple demo that highlights some of the more advanced Entity SQL features. Show a couple of canonical functions in action (i.e. GetDate(), IndexOf()), and also show the use of a store-specific function, after importing a namespace (i.e. using SqlServer). Show how to perform paging using both the TOP and the SKIP/LIMIT approaches. Then show a simple example of how to group a Entity SQL query. Id recommend using eSqlBlast for this demo to make it much easier.

12Object ServicesTransactionsOptimistic ConcurrencyCompiled Queries

Estimated Time: 5 minutes

Talking Points: When consuming an EDM using Object Services, there are common tasks that are needed in any data access platform: Optimistic Concurrency Transactions Object Services provides full support for performing concurrency checking and creating transactions. In addition, Object Services allows you to create a compiled query, that increases performance when executing a query that will be run repeatedly.13DemoLINQ To EntitiesEstimated Time: 10 minutes

Description:This demo needs to show a simple example of creating a transaction boundary around a set of Object Services operations. Then show how to perform optimistic concurrency and handling an error when submitting changes. Then show a simple example of creating a compiled query that can be executed repeatedly.

14ADO.NET MetadataExposes metadataExposed fromObjectContextEdmConnectionEdmDataReaderMetadata WorkspaceO-SpaceOC-SpaceC-SpaceCS-SpaceS-SpaceEstimated Time: 2 minutes

Talking Points: Within your EDM is a lot of metadata describing the various layers of the model, and how they are mapped together. There will be times where it is valuable to be able to examine the metadata of an EDM and query the individual dataspaces. The O-Space is your object model space, which describes the shape of your CLR objects that represent your conceptual model. The OC-Space is the object-to-conceptual space, which describes the mapping between your object and conceptual models. The C-Space is your conceptual space, which describe the shape of your conceptual model. The CS-Space is your conceptual-to-storage space, which describes the mapping between your conceptual and storage models. The S-Space is your storage space, which describes the shape of your underlying database model. ADO.NET Metadata allows you to peer into each data space of an EDM and query its metadata. Metadata is exposed through an ObjectContext, EdmConnection, and EdmDataReader, making it very readily available regardless which data query option youre using to consume your EDM.15DemoADO.NET MetadataEstimated Time: 10 minutes

Description:This demo should show a very simple example of how you can query your EDMs metadata through the ObjectContext class. As an example, you can show how to retrieve a list of all functions available in the C-Space.

16SummaryEntity Data ModelAdvanced Mapping

Advanced queryingEntity SQLObject Services

ADO.NET Metadata

Estimated Time: 2 minutes

Talking Points: In summary We discussed how to perform advance mappings with an EDM, including:Complex Types Table-Per-Hierarchy Table-Per-Type Defining Queries We saw how to perform advance queries using Entity SQL, leveraging canonical and store-specific functions, as well as paging and grouping functionality. We saw how to use transactions and optimistic concurrency in conjunction with Object Services as well as how to create compiled queries. Finally, we showed how to examine the metadata of your EDM from within code.17