1 / 40

Entity Framework

Jorge Fioranelli | b: blog.jorgef.net | e: jorge.fioranelli@gmail.com. Entity Framework. Agenda. Introduction Basic Concepts POCOs Code Generation Unit Testing N-Tiers CTP4. Versions. Version 1. EF 3.5. .Net 3.5 sp1. Version 2. EF 4 (aka EF ). .Net 4. EF and LINQ2SQL.

wolfe
Télécharger la présentation

Entity Framework

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. Jorge Fioranelli | b: blog.jorgef.net | e: jorge.fioranelli@gmail.com Entity Framework

  2. Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4

  3. Versions Version 1 EF 3.5 .Net 3.5 sp1 Version 2 EF 4 (aka EF ) .Net 4

  4. EF and LINQ2SQL Data Programmability Team Data Programmability Team Entity Framework Entity Framework ESQL ESQL LINQ Data Programmability Team Entity Framework ESQL LINQ LINQ2SQL LINQ Language Development Team Language Development Team LINQ2SQL LINQ2SQL LINQ LINQ

  5. Future “If you’re a .NET developer, Entity Framework is where we’re going. We’re there. Get on board, it’s time.” Don Box, PDC 2009 Source: Programming Entity Framework 2nd Edition, Julia Lerman

  6. New EF4 Features • Customizable Code-Generation • Design-time support for Complex Types • Pluralization • Multi-Targeting Support • Persistence Ignorance & POCO • Self-Tracking Entities & N-Tier support • Application Patterns and Testability • Foreign Key Associations • Lazy Loading • More LINQ Operators in LINQ to Entities • CreateDatabase and DDL Provider Services • Model Defined Functions LINQ support • EntityDataSource support for QueryExtender, POCO and FKs • ObjectMaterializedevent • Generated SQL improvements for better performance and readability • Ad-hoc native query support

  7. Entity Data Model • Entity Framework evolves from “Entity Relationship Modeling” (Dr. Peter Chen – 1976) • In 2006 Microsoft Research published the first paper on “Entity Data Model” Entity Data Model (EDMX) Tables Storage Model (SSDL) Conceptual Model (CSDL) Objects Mappings (MSL)

  8. Database, Model or Code First Database First Code First (CTP4) Code DB Model First Model Model Model (no visual) DB Code DB Code

  9. Introduction Demo 1

  10. Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4

  11. Associations • Early Load • Deferred Load • Explicit Load • Lazy Load • context.Products.Include("Category") • context.Products.Include(p => p.Category) //CTP4 • product.CategoryReference.Load() • context.LoadProperty(product, p => p.Category) • product.Category

  12. Updating Entities Change Tracking Query Result product DB context Name Color Update (on SaveChanges) • var product = ... //query • product.Name = "New Name"; • product.Color = "Blue"; • context.SaveChanges();

  13. Inserting Entities Add Object DB context newCategory Insert (on SaveChanges) Change Tracking varnewCategory = newCategory() {...}; context.Categories.AddObject(newCategory); context.SaveChanges();

  14. Inserting Entities by Association Change Tracking Query Result product DB context newCategory Category CategoryID Insert (on SaveChanges) Add Association • var product = ... //query • varnewCategory = newCategory(); • product.Category = newCategory; • context.SaveChanges();

  15. Deleting Entities Change Tracking Query Result DB context orderDetail Delete (on SaveChanges) Delete Object • varorderDetail= ... //query • context.OrderDetails.DeleteObject(orderDetail); • context.SaveChanges();

  16. Attaching Entities Attach DB context existingCategory Update (on SaveChanges) Change Tracking • varexistingCategory= newCategory() {...}; • existingCategory.Name = "Old Name";// no change tracking • context.ProductCategories.Attach(existingCategory); • existingCategory.Name= "New Name"; • context.SaveChanges();

  17. Basic Operations Demo 2

  18. Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4

  19. Plain Old CLR Objects (POCOs) EntityObject EntityCollection EntityReference Entity Property Change Events Relationship Manager VS POCO Entity

  20. Plain Old CLR Objects Demo 3

  21. Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4

  22. Code Generation Database First Code First (CTP4) Code DB Model First Model Model Model (no visual) DB Code DB Code

  23. Code Generation Demo 4

  24. Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4

  25. Unit Testing common unit testing scope Service Model UnitOfWork View DB Controller Entity Repository unit tests for Repositories

  26. Unit Testing ObjectContext IObjectSet<T> ObjectQuery<T> NorthwindCtx Product Repository ObjectSet <Product> ObjectSet<T> ObjectSet <Category> Category Repository

  27. Unit Testing ObjectContext INorthwindCtx IObjectSet<T> ObjectQuery<T> NorthwindCtx Product Repository IObjectSet <Product> ObjectSet<T> IObjectSet <Category> Category Repository

  28. Unit Testing Demo 5

  29. Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4

  30. Self-tracking Entities EF change tracking No Change Tracking Query Result Change Tracking DB product product Client context Update? Self-tracking Entities Change Tracking Query Result Change Tracking DB product product Client context Update State + Changes are transferred

  31. N-Tiers with POCOs Self-tracking Entities Change Tracking Query Result Change Tracking DB product product Client context Update State + Changes are transferred POCO Entities Query Result DB product product Client context Just POCO’s state is transferred Update

  32. N-Tiers Demo 6 – Demo 7

  33. Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4

  34. Code First (Community Technical Preview 4) Database First Code First (CTP4) Code DB Model First Model Model Model (no visual) DB Code DB Code

  35. Productivity Improvements(Community Technical Preview 4) ObjectContext DbContext NorthwindCtx NorthwindCtx ObjectSet <Product> DbSet <Product> ObjectSet <Category> DbSet <Category> ObjectContext

  36. Community Technical Preview 4 Demo 8

  37. Out of Scope • Table Splitting • Inheritance • Complex types • Entity SQL • Transactions • Concurrency • SPs, Views and Functions

  38. Resources • Programming Entity Framework 2nd Edition – Julia Lerman • ADO.NET Team Blog http://blogs.msdn.com/adonet • EF Design Blog http://blogs.msdn.com/efdesign • Data Developer Center http://msdn.com/data • Entity Framework Forum http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework

  39. Resources • PDC 2010 video: • Code First Development with Entity Framework http://player.microsoftpdc.com/Session/68cfa011-c399-4151-ad9f-748d8723a19d • Tech-ed 2010 videos: • Data Development GPS http://www.msteched.com/2010/NorthAmerica/DEV324 • Overview of the Microsoft ADO.NET Entity Framework 4.0 http://www.msteched.com/2010/NorthAmerica/DEV205 • Deep Dive into Microsoft ADO.NET Entity Framework http://www.msteched.com/2010/NorthAmerica/DEV305 • PDC 2009 video: • Evolving ADO.NET Entity Framework in .NET 4 and Beyond http://www.microsoftpdc.com/2009/FT10

  40. Jorge Fioranelli | b: blog.jorgef.net | e: jorge.fioranelli@gmail.com Questions

More Related