1 / 28

Entity Framework 4 Deep Dive

Required Slide. SESSION CODE: DEV305. Entity Framework 4 Deep Dive. Tim Laverty – timlav@microsoft.com Diego Vega – diego.vega@microsoft.com Program Managers Microsoft Corporation. Goals. Answers to common “depth” questions Focus on real-world scenarios

ambrose
Download Presentation

Entity Framework 4 Deep Dive

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Required Slide SESSION CODE: DEV305 Entity Framework 4 Deep Dive Tim Laverty – timlav@microsoft.com Diego Vega – diego.vega@microsoft.com Program Managers Microsoft Corporation

  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. Mapping Tim LavertyProgram ManagerMicrosoft DEMO

  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 Business logic, UI, etc. Data Access Framework Repository Data Access Framework Database Database

  8. Repository Pattern using Entity Framework Diego Vega Program Manager Microsoft Corporation Demo

  9. 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

  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 Business logic, UI, etc. Real Repository Fake Repository Repository Fake for context interface ObjectContext Database In-memory data 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 Presentation Business logic Data Access Database

  14. Self-Tracking Entities Diego Vega Program Manager Entity Framework Demo

  15. 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

  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 Tim LavertyProgram ManagerMicrosoft DEMO

  18. 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)

  19. 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

  20. 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

  21. 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!

  22. 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

  23. Required Slide Resources Learning • Sessions On-Demand & Community • Microsoft Certification & Training Resources www.microsoft.com/teched www.microsoft.com/learning • Resources for IT Professionals • Resources for Developers • http://microsoft.com/technet • http://microsoft.com/msdn

  24. Required Slide Complete an evaluation on CommNet and enter to win!

  25. Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st http://northamerica.msteched.com/registration You can also register at the North America 2011 kiosk located at registrationJoin us in Atlanta next year

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

More Related