420 likes | 634 Views
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.
E N D
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 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
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
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
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)
Database, Model or Code First Database First Code First (CTP4) Code DB Model First Model Model Model (no visual) DB Code DB Code
Introduction Demo 1
Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4
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
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();
Inserting Entities Add Object DB context newCategory Insert (on SaveChanges) Change Tracking varnewCategory = newCategory() {...}; context.Categories.AddObject(newCategory); context.SaveChanges();
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();
Deleting Entities Change Tracking Query Result DB context orderDetail Delete (on SaveChanges) Delete Object • varorderDetail= ... //query • context.OrderDetails.DeleteObject(orderDetail); • context.SaveChanges();
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();
Basic Operations Demo 2
Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4
Plain Old CLR Objects (POCOs) EntityObject EntityCollection EntityReference Entity Property Change Events Relationship Manager VS POCO Entity
Plain Old CLR Objects Demo 3
Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4
Code Generation Database First Code First (CTP4) Code DB Model First Model Model Model (no visual) DB Code DB Code
Code Generation Demo 4
Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4
Unit Testing common unit testing scope Service Model UnitOfWork View DB Controller Entity Repository unit tests for Repositories
Unit Testing ObjectContext IObjectSet<T> ObjectQuery<T> NorthwindCtx Product Repository ObjectSet <Product> ObjectSet<T> ObjectSet <Category> Category Repository
Unit Testing ObjectContext INorthwindCtx IObjectSet<T> ObjectQuery<T> NorthwindCtx Product Repository IObjectSet <Product> ObjectSet<T> IObjectSet <Category> Category Repository
Unit Testing Demo 5
Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4
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
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
N-Tiers Demo 6 – Demo 7
Agenda • Introduction • Basic Concepts • POCOs • Code Generation • Unit Testing • N-Tiers • CTP4
Code First (Community Technical Preview 4) Database First Code First (CTP4) Code DB Model First Model Model Model (no visual) DB Code DB Code
Productivity Improvements(Community Technical Preview 4) ObjectContext DbContext NorthwindCtx NorthwindCtx ObjectSet <Product> DbSet <Product> ObjectSet <Category> DbSet <Category> ObjectContext
Out of Scope • Table Splitting • Inheritance • Complex types • Entity SQL • Transactions • Concurrency • SPs, Views and Functions
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
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
Jorge Fioranelli | b: blog.jorgef.net | e: jorge.fioranelli@gmail.com Questions