Download presentation
Presentation is loading. Please wait.
1
Entity Framework 4 Deep Dive
Tech Ed North America 2010 5/15/2018 4:47 AM Required Slide SESSION CODE: DEV305 Entity Framework 4 Deep Dive Tim Laverty – Diego Vega – Program Managers Microsoft Corporation © 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.
2
Goals Answers to common “depth” questions
Focus on real-world scenarios Shortcomings of EF and workarounds
3
Agenda 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?
4
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
5
Tech Ed North America 2010 Tim Laverty Program Manager Microsoft
Mapping Tim Laverty Program Manager Microsoft DEMO © 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.
6
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
7
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 Data Access Framework Database Business logic, UI, etc. Repository Data Access Framework Database
8
Repository Pattern using Entity Framework
Diego Vega Program Manager Microsoft Corporation Demo
9
Repository Pattern – Recap
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
10
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 Repository ObjectContext Database Business logic, UI, etc. Real Repository Fake for context interface In-memory data Fake Repository In-memory data
11
Testability using Entity Framework
Diego Vega Program Manager Microsoft Corporation Demo
12
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!
13
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 Data Access Database Presentation Business logic
14
Self-Tracking Entities
Diego Vega Program Manager Entity Framework Demo
15
N-Tier with Entity Framework – Recap
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
16
How do I improve EF perf? – Factors
Query complexity and use Model size and complexity Inheritance hierarchies Database Schema Design
17
Strategies for making EF queries run faster
Tech Ed North America 2010 5/15/2018 4:47 AM Strategies for making EF queries run faster Tim Laverty Program Manager Microsoft DEMO © 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.
18
EF load steps for startup and on first query?
EF Startup Loads Model Metadata Generates EF Query Views (or upon first query) First Time Query Execution EF Query Translation EF Query Caching Manual with LINQ queries. Result Shaper Caching
19
How do I improve EF query perf – Recap
Round trips Lazy Loading Multiple Queries Connection Overuse Danger! Use Include No Updates! Use NoTracking Expensive 1st time Cached Compile Query Large data No Updates! Projection Separate Queries EF offers fixup Stored Procs/Views Not Composable
20
Related Content Required Slide
Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Tech Ed North America 2010 5/15/2018 4:47 AM 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 | Rm 288 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 © 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.
21
Additional Resources and Announcements
MSDN Data Developer Center: ADO.NET Team Blog: OData Blog: WCF Data Services Team Blog: EF Design Blog: Data Platform Development Forums: and click on the “Forums” tab Patterns and Testability: Come to the Data Development station in the DAT Track Area (yellow section of the TLC) to register for a Zune HD give-away!
22
Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub. Tech Ed North America 2010 5/15/2018 4:47 AM Track Resources Visual Studio – Soma’s Blog – MSDN Data Developer Center – ADO.NET Team Blog – WCF Data Services Team Blog – EF Design Blog – © 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.
23
Resources Learning Required Slide www.microsoft.com/teched
Tech Ed North America 2010 5/15/2018 4:47 AM Required Slide Resources Learning Sessions On-Demand & Community Microsoft Certification & Training Resources Resources for IT Professionals Resources for Developers © 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.
24
Complete an evaluation on CommNet and enter to win!
Tech Ed North America 2010 5/15/2018 4:47 AM Required Slide Complete an evaluation on CommNet and enter to win! © 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.
25
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 registration Join us in Atlanta next year
26
Tech Ed North America 2010 5/15/2018 4:47 AM
© 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. © 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.
27
Appendix
28
Required Slide Tech Ed North America 2010 5/15/2018 4:47 AM
© 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.