On Sept 27, 2010, Julie Lerman, Rachel Appel and Steve Bohlen presented a full day of Entity Framework presentations to help developers get started with EF. The presentations begin with "why entity framework" and end with creating better architectures when using EF.
10. Model in Entity Framework Model First Database First New Database Legacy Database Code First NYC Entity Framework Firestarter, Sept 27th 2010 “M”
11. Entity Framework Conceptual Model in your Apps UI DB Conceptual Model of your Domain App Logic Provider Query Track Changes Repository Updates to DB Business Objects NYC Entity Framework Firestarter, Sept 27th 2010
12. Time to Code A little modeling… A little querying… A little editing… A little saving… NYC Entity Framework Firestarter, Sept 27th 2010
13. FAQS Stored Procedure Support Use Views & Stored Procs – no table access Update the model when DB changes Mappings are highly customizable Inheritance Many to Many Merge tables Split tables etc Pass through queries & commands NYC Entity Framework Firestarter, Sept 27th 2010
15. Why Entity Framework Summary Microsoft’s in-the-box ORM Focus of Microsoft/.NET data access Use any back end db (with a provider) Use in any type of .NET application Backbone of many Microsoft technologies NYC Entity Framework Firestarter, Sept 27th 2010
16. Part 2:What’s new in Entity Framework 4 EF NYC Firestarter Steve Bohlen unhandled-exceptions.com/blog twitter @sbohlen
17. Long Form: Comprehensive NYC Entity Framework Firestarter, Sept 27th 2010 Model-first development Automatic pluralization Foreign keys in models POCO class support Lazy loading T4 Code Generation Template customization IObjectSet Virtual SaveChanges ObjectStateManagercontrol Self-tracking entities SQL generation improvements More LINQ operator support LINQ extensibility ExecuteStoreQuery ExecuteStoreCommand SPROC import improvements Model defined functions WPF designer integration Code-First development (Feature CTP)
18. Short Form: Highlights Designer Improvements Foreign Keys New Code Generation Persistence Ignorance Change Tracking for WCF Better n-tier Support Lazy Loading Code-Only Development
19. Designer Improvements Pluralization Support Generate Database from Model vs. only Reverse-Engineering in v1 Improved SPROC Support SPROCs can return only entities in v1 SPROCs can return strongly-typed projections in v4
20. Foreign Key Support ERM vs exposed FKs (it’s a CHOICE via the dialog box) Does NOT replace the Nav properties DataContext is able to leverage this for some efficiencies Changing either the FK or the Nav property changes the other value (due to code-gen support)
21. Code Generation Model templates Classes Code Generation Engine now T4 instead of proprietary engine in v1 Tools support/syntax support resources review a quick look at the T4 syntax T4 Editor add-in
22. (better) Persistence Ignorance EntityObject inheritance NOT required Snapshot-based No lazy loading support in this approach No Change Tracking must call ObjectContext.DetectChanges( ) ObjectContext.Save( ) implicitly calls .DetectChanges( ) Proxy-based virtual keyword is the ‘flag’ for this behavior virtual on all change-tracking and lazy-load virtual on just navigation props only lazy-loading Several Canned Templates
23. WCF and Self-Tracking Entities T4 template for self-tracking entities Passing objects across serialization boundaries
24. Lazy Loading v1 supported only explicit load of lazy collections ObjectContext.ContextOptions.LazyLoadingEnabled = true by default in NEW models, but NOT in imported v1 models Behavior scoped at the ObjectContext NOT at property level for entities Toggle-able before and after operations for query-specific granular lazy-loading behavior
25. Explicit State Management ObjectContext.ApplyCurrentValues( ) Formerly ApplyPropertyChanges( ) in v1 ObjectContext.ApplyOriginalValues( ) ObjectStateEntry.ChangeState( ) Unchanged/Added/Modified/Deleted ObjectStateManager.ChangeRelationshipState( )
26. Code-Only Support ‘Feature CTP’ Classes Entities Entities Database (optionally) No Model Necessary Heavily Convention over Configuration
27. Miscellaneous Model-Defined Functions ObjectSet (collections of entities) Complex Type Support Query optimization (E-SQL, LINQ2Entities) ExecuteCommand/Defined Queries/Model Functions Designer support for more features without direct editing of .csdl, .ssdl, .edml
28. Part 4: Getting Started with POCOs in EF4 EF NYC Firestarter Julie Lerman thedatafarm.com/blog twitter @julielerman
31. Benefits Use existing classes Loose Coupling Classes have no dependency on EF Persistence Ignorance Unit Testing Repositories & Unit Of Work Patterns
32. ObjectContext Manages Entities NYC Entity Framework Firestarter, Sept 27th 2010 POCO “Snapshot”4.0 EntityObject (3.5 & 4.0) ObjectContext Person Object State info State info Order Object State info Detail Object State info Detail Object State info Order Object State info Detail Object State info Detail Object
33. POCO Support Basics Turn off code generation from EDMX “Convention” will auto-map classes/entities NYC Entity Framework Firestarter, Sept 27th 2010
34. POCO and Other EF Features Complex Types Use a class (no structs) to represent the type Explicit Loading ObjectContext.LoadProperty(myObj,”PropertyName”) ObjectContext.LoadProperty(myObj, o=>o.property) Two-Way Navigation Must be coded into classes NYC Entity Framework Firestarter, Sept 27th 2010
36. ObjectContext Manages Entities NYC Entity Framework Firestarter, Sept 27th 2010 POCO “Snapshot”4.0 POCO + Dynamic Runtime Proxy (4.0) EntityObject (3.5 & 4.0) ObjectContext Person Object State info State info Order Object State info Detail Object State info Detail Object State info Order Object State info Detail Object State info Detail Object
39. RelationshipsPerson class F5 Mark all Entity (model) Properties virtual Provides features similar to EntityObject Change notification Relationship management Lazy Loading
40. Rules for Proxy Notification All entity properties shall be virtual Though shalt use ICollection<T> Creation shall be done withObjectContext.CreateObject<T> Classes shall not be sealed Navigation properties shall not be sealed Classes shall not be abstract Classes shall have a parameter-less constructor
41. Code Gen POCOs from Model T4 T 4 Text Template Transformation Toolkit
42. EF POCO Summary POCO support is new to EF4 Alternative to EntityObjects Simple snapshot POCOs or use proxies Use or even customize T4 template for code generation Enable many coding/architectural possibilities NYC Entity Framework Firestarter, Sept 27th 2010
43. Part 5: Disconnected Strategies for ASP.NET & Services EF NYC Firestarter Julie Lerman thedatafarm.com/blog twitter @julielerman
44. Agenda EF Challenges in Server Aps Review Options with EF EF4 Tools to the Rescue POCOs and Repositories Leveraging Foreign Keys Putting it all together
45. Change Tracking Across Tiers ObjectContext ObjectStateEntry SO1 EntityKey Original ValuesCurrent Values Other ∆ Info ObjectStateEntry SO2 EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info detach entities ObjectStateEntry LIA EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info Original Property Values EntityState Values ObjectStateEntry LIB EntityKeyOriginal ValuesCurrent ValuesOther ∆ Info
46. SaveChanges Fails Across Post Backs Page Request Post Back #1 Post Back #2 New Page Class New Page Class New Page Class Create New ObjectContext Create NewObjectContext Create NewObjectContext Get Entities Process Request Get Entities Build HTML using Data & ASP.NET Markup Build HTML using Data & ASP.NET Markup Build HTML using Data & ASP.NET Markup Destroy Page and Dependents Destroy Page and Dependents Destroy Page and Dependents
47. The Server Side Spectrum with EF Dynamic Data forEntities ObjectDataSource WCF & Self-Tracking Ents EntityDataSource MVC WCF Data Services Dev WCF ASP.NET n-Tier Architect
48. EF4 Changes to the Rescue Foreign Keys Big help for selected list values on postback State Methods ApplyCurrentValues, ApplyOriginalValues ChangeState ChangeRelationshipState (when no FKs) POCO Classes & T4 Generation Self-Tracking Entities for use with WCF
51. Disconnected Strategies Summary EF has inherent challenges across tiers New tools in EF4 to enable disconnected NYC Entity Framework Firestarter, Sept 27th 2010
52. Part 6:Writing Testable/Maintainable Apps with EF EF NYC Firestarter Steve Bohlen unhandled-exceptions.com/blog twitter @sbohlen Julie Lerman thedatafarm.com/blog twitter @julielerman
53. Agenda Setting the stage Refactor a demo-worthy MVC app Explore an existing solution with more complexity
54. “-ilities” Benefits: Extensibility Adaptability Testability Maintainability Quality Why should You Care? Persistence Ignorance (PI) Future-Proofing your investment NYC Entity Framework Firestarter, Sept 27th 2010
55. IObjectSet Abstraction IObjectSet Collection-like behavior Add, Attach, Delete, etc. ObjectSet Concrete class Implements IObjectSet Inherits ObjectQuery public class ObjectSet:ObjectQuery,IObjectSet
56. EF4 Goodness Makes This Work NYC Entity Framework Firestarter, Sept 27th 2010 POCOs + T4 Code Gen Disconnected Methods IObjectSet
57. Classes PI/Repositories System.Data.Entity Fake ObjectSet Fake Context & Data A EDM & ObjectContext Fake Context & Data B Unit of Work Class Repositories Class Repositories Class Repositories Repositories UI Tests
58. Resources LearnEntityFramework.com/resources thedatafarm.com/blog/tags/ef4 blogs.msdn.com/adonet, efdesign, dsimmons, alexj, more... msdn.microsoft.com/data Gateway to whitepapers, forums, documentation Lots of bloggers on EF now! Google! -Bing! NYC Entity Framework Firestarter, Sept 27th 2010