Entity framework 4 deep dive
1 / 28

- PowerPoint PPT Presentation

  • Updated On :

Required Slide. SESSION CODE: DEV305. Entity Framework 4 Deep Dive. Tim Laverty – [email protected] Diego Vega – [email protected] Program Managers Microsoft Corporation. Goals. Answers to common “depth” questions Focus on real-world scenarios

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about '' - ambrose

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Entity framework 4 deep dive l.jpg

Required Slide


Entity Framework 4 Deep Dive

Tim Laverty – [email protected]

Diego Vega – [email protected]

Program Managers

Microsoft Corporation

Goals l.jpg

  • Answers to common “depth” questions

  • Focus on real-world scenarios

  • Shortcomings of EF and workarounds

Agenda l.jpg

  • How do I:

    • Map classes to database structures?

    • Use patterns such as Repository?

    • Write unit tests for apps using EF?

    • Use EF in disconnected scenarios?

    • Improve EF query perf?

How do i map classes to database structures l.jpg
How do I map classes to database structures?

  • EF allows flexibility in mapping relational structures to OO structures

  • Common mapping questions:

    • Inheritance

    • Mapping a single table to multiple entities

    • Mapping Stored Procedures

Mapping l.jpg


Tim LavertyProgram ManagerMicrosoft


Ef mapping capabilities l.jpg
EF Mapping Capabilities

  • Inheritance

    • Table per Hierarchy

    • Table per Type

    • Table per Concrete Type

    • Hybrids

  • Many entities to one table

  • Stored Procedures

  • Many tables to one entity

  • Abstract Entities

  • Associations within EntitySets

  • Associations across EntitySets

  • Store-side discriminators

  • EDM-side discriminators

  • QueryViews, Defining Query, CommandText

Repository pattern overview l.jpg
Repository Pattern – Overview

  • Why:

    • Cleaner separation of concerns

    • Greater control

    • Easier testability

  • How:

    • Simple class with simple interface

    • Pretends to be just a collection

    • Runs queries & updates on the application’s behalf

Business logic, UI, etc.

Data Access Framework


Data Access Framework



Repository pattern using entity framework l.jpg

Repository Pattern using Entity Framework

Diego Vega

Program Manager

Microsoft Corporation


Repository pattern recap l.jpg

Repositories are easy with EF!

Best Practices:

Make the repository’s interface as simple as possible

Create separate repositories for each “aggregate”

Keep other layers “persistence ignorant”

Multiple repositories can share same ObjectContext as “unit of work”

Popular additions:

Query methods return IQueryable<T>

Using “specification pattern”, e.g. repository.Find(c=>c.Id == id)

Repository<T> base class

Repository Pattern – Recap

Testability using entity framework l.jpg
Testability using Entity Framework

  • Why:

    • Test business logic, not database

    • Lightning fast unit tests!

  • How: swap data access code with in-memory test doubles

    • Choice #1: replace repository

    • Choice #2: replace context interface

      • LINQ only

Business logic, UI, etc.

Real Repository

Fake Repository


Fake for context interface



In-memory data

In-memory data

Testability using entity framework11 l.jpg

Testability using Entity Framework

Diego Vega

Program Manager

Microsoft Corporation


Testability with entity framework recap l.jpg
Testability with Entity Framework – Recap

  • Testability is easy with EF4!

  • Best practices:

    • Build simple repository interface

    • Swap repository with in-memory test double

    • Alternate route: define simple interface for context

    • Still run tests against database often

  • Things to avoid:

    • Don’t try to mock the data access framework!

N tier with entity framework l.jpg
N-Tier with Entity Framework

  • Why:

    • App split by machine boundary

    • Need to serialize object graphs

    • Save changes made elsewhere

  • How: use WCF with

    • Self-Tracking Entities

    • WCF Data Services, OData

    • WCF RIA Services

    • Your own data transfer objects


Business logic

Data Access


Self tracking entities l.jpg

Self-Tracking Entities

Diego Vega

Program Manager

Entity Framework


N tier with entity framework recap l.jpg

EF4 N-Tier improvements:

New low level state management API

Self-Tracking Entities

Best practices:

Use standard change tracking when possible

Use Self-Tracking Entities if sharing entity types is acceptable

Create data transfer objects when low coupling is needed

Other technologies to learn about:

WCF Data Services - bit.ly/learndataservices

WCF RIA Services - bit.ly/learnriaservices

N-Tier with Entity Framework – Recap

How do i improve ef perf factors l.jpg
How do I improve EF perf? – Factors

  • Query complexity and use

  • Model size and complexity

    • Inheritance hierarchies

  • Database Schema Design

Strategies for making ef queries run faster l.jpg

Strategies for making EF queries run faster

Tim LavertyProgram ManagerMicrosoft


Ef load steps for startup and on first query l.jpg
EF load steps for startup and on first query?

  • First Time Query Execution

  • EF Query Translation

  • EF Query Caching

    • Manual with LINQ

    • queries.

  • Result Shaper Caching

  • EF Startup

  • Loads Model Metadata

  • Generates EF Query Views

  • (or upon first query)

How do i improve ef query perf recap l.jpg
How do I improve EF query perf – Recap

  • Round trips

  • Multiple Queries

  • Overuse Danger!

  • No Updates!

  • Expensive 1st time

  • Cached

  • Large data

  • No Updates!

  • Compile Query

  • Projection

  • Separate Queries

  • Stored Procs/Views

  • Lazy Loading

  • Connection

  • Use Include

  • Use NoTracking

  • EF offers

  • fixup

  • Not Composable

Related content l.jpg

Required Slide

Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session.

Related Content

  • DEV324: Data Development GPS: Guidance for Choosing the Right Data Access Technology – Monday | 1:00 – 2:15 PM | Rm 383

  • DEV208: Open Data for the Open Web – Tuesday | 5:00 – 6:15 PM | Rm 279

  • DEV205: Overview of the Microsoft ADO.NET Entity Framework – Wednesday | 1:30 – 2:45 PM | Auditorium B

  • DEV303: Building RESTful Applications with the Open Data Protocol – Wednesday | 3:15 – 4:30 PM | Rm 283

  • DEV323: Best Practices: Creating OData Services Using WCF Data Services – Wednesday | 5:00 – 6:15 PM | Rm 295

  • ARC306: Open Data for the Enterprise – Thursday | 8:00 – 9:15 AM | Rm288

  • BOF05-DV: Microsoft ADO.NET Entity Framework 4.0: What’s Your Take? – Thursday | 9:45-11:00 AM

  • DEV13-HOL: Building Applications and Services Using Open Data Protocol

  • DEV11-HOL: Using the Entity Framework in Microsoft .NET Framework 4.0 and Microsoft Visual Studio 2010

  • TLC-77: Microsoft SQL Server R2 Data Development (ADO.NET, OData, XML) – TLC Yellow

Additional resources and announcements l.jpg
Additional Resources and Announcements

  • MSDN Data Developer Center: http://msdn.com/data

  • ADO.NET Team Blog: http://blogs.msdn.com/adonet

  • OData Blog:http://odata.org/blog

  • WCF Data Services Team Blog: http://blogs.msdn.com/astoriateam

  • EF Design Blog: http://blogs.msdn.com/efdesign

  • Data Platform Development Forums:http://msdn.com/dataand click on the “Forums” tab

  • Patterns and Testability:

    http://bit.ly/learnef4test, http://bit.ly/ef4wpfsample

Come to the Data Development station in the DAT Track Area (yellow section of the TLC) to register for a Zune HD give-away!

Track resources l.jpg

Required Slide

Track PMs will supply the content for this slide, which will be inserted during the final scrub.

Track Resources

  • Visual Studio – http://www.microsoft.com/visualstudio/en-us/

  • Soma’s Blog – http://blogs.msdn.com/b/somasegar/

  • MSDN Data Developer Center – http://msdn.com/data

  • ADO.NET Team Blog – http://blogs.msdn.com/adonet

  • WCF Data Services Team Blog – http://blogs.msdn.com/astoriateam

  • EF Design Blog – http://blogs.msdn.com/efdesign

Resources l.jpg

Required Slide



  • Sessions On-Demand & Community

  • Microsoft Certification & Training Resources



  • Resources for IT Professionals

  • Resources for Developers

  • http://microsoft.com/technet

  • http://microsoft.com/msdn

Slide24 l.jpg

Required Slide

Complete an evaluation on CommNet and enter to win!

Slide25 l.jpg

Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st


You can also register at the North America 2011 kiosk located at registrationJoin us in Atlanta next year

Slide26 l.jpg

© 2010 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.