26
ADO.NET Entity Framework in Microsoft Visual Studio 2010 and Microsoft .NET Framework 4 Eric Nelson [email protected] http://geekswithblogs.net/iupdateable http://twitter.com/ericnel Developer Evangelist Microsoft UK Advert Alert http://ukazure.ning.com

Entity Framework 4 In Microsoft Visual Studio 2010

Embed Size (px)

DESCRIPTION

slides from March 2010 www.devweek.com

Citation preview

Page 1: Entity Framework 4 In Microsoft Visual Studio 2010

ADO.NET Entity Framework in Microsoft Visual Studio 2010 and

Microsoft .NET Framework 4Eric Nelson

[email protected]://geekswithblogs.net/iupdateable

http://twitter.com/ericnel Developer Evangelist

Microsoft UK

Advert Alert http://ukazure.ning.com

Page 2: Entity Framework 4 In Microsoft Visual Studio 2010

2

Agenda

• Swift intro to Entity Framework• Entity Framework 4.0 Drill Down– Lots of demos

Page 3: Entity Framework 4 In Microsoft Visual Studio 2010

3

Why an ORM? Why Entity Framework?

Page 4: Entity Framework 4 In Microsoft Visual Studio 2010

4

Why an ORM? Why Entity Framework?

Page 5: Entity Framework 4 In Microsoft Visual Studio 2010

5

ADO.NET Entity Framework Recap

• Tools and services to create an Entity Data Model (EDM)– Conceptual to Mapping to Storage

• Tools and services for consuming an Entity Data Model– LINQ to Entities, Object Services and Entity SQL

• Together they address the “impedance mismatch”

ConceptualConceptual

MappingMapping

StorageStorage

Entity Data ModelEntity Data Model

Rows in Tables

Rows in Tables

Objects in ApplicationsObjects in

Applications

Page 6: Entity Framework 4 In Microsoft Visual Studio 2010

Model First and Templated Code Generation

Page 7: Entity Framework 4 In Microsoft Visual Studio 2010

7

Entity Framework 1.0 Features

• Generate model from database schema– Very rich mapping layer– Inheritance, rename, aggregate, filter

• Simplified CRUD operations• LINQ querying*• Worked with many databases• Database agnostic query language• SP support for data retrieval/modification*• Designer*• Strategic!

Page 8: Entity Framework 4 In Microsoft Visual Studio 2010

8

Entity Framework 1.0 - Too many pain points

• The designer!• Model First not supported• Poor support for Stored Procedures• No Pluralization/Singularization• Foreign Keys hidden• Lazy Loading not supported• Missing LINQ Operators vs LINQ to SQL• Generated SQL unreadable• No support for Plain Old CLR Objects• N-Tier difficult

Page 9: Entity Framework 4 In Microsoft Visual Studio 2010

Entity Framework 4.0

Page 10: Entity Framework 4 In Microsoft Visual Studio 2010

10

New in ADO.NET Entity Framework 4.0

• Model-first development• Automatic pluralization• Foreign keys in models• POCO class support (Add-in)• Lazy loading• T4 Code Generation• Template customization• IObjectSet• Virtual SaveChanges• ObjectStateManager control

• 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)

Page 11: Entity Framework 4 In Microsoft Visual Studio 2010

11

Entity Framework 4.0 - Removing the friction

• Better Tools and Design Experience• More powerful/flexible runtime• And– Persistence Ignorance*– N-Tier– Code First*

• Note:– * Delivered in a separate download

Page 12: Entity Framework 4 In Microsoft Visual Studio 2010

12

Better Tools and Design Experience

• Model First• Templated code generation• Stored Procedures• Pluralization/Singularization • Complex Types• Better delete and search

Page 13: Entity Framework 4 In Microsoft Visual Studio 2010

Model First and Templated Code Generation

Page 14: Entity Framework 4 In Microsoft Visual Studio 2010

14

More powerful/flexible runtime

• Lazy loading• Foreign Keys surfaced• More complete LINQ implementation• ExecuteStoreQuery• EntityFunctions and SqlFunctions• Improvements to generated SQL

Page 15: Entity Framework 4 In Microsoft Visual Studio 2010

Deferred Loading, Generated SQL

Page 16: Entity Framework 4 In Microsoft Visual Studio 2010

http://blogs.msdn.com/adonet/archive/2009/08/05/improvements-to-the-generated-sql-in-net-4-0-beta1.aspx

Page 17: Entity Framework 4 In Microsoft Visual Studio 2010

17

Feature Pack / Add-Ins

• Separate downloads• Feature Pack– Code First support

• Add-In– T4 Template for Persistence Ignorance/POCO

Page 18: Entity Framework 4 In Microsoft Visual Studio 2010

18

Persistence Ignorance/POCO

• First class support for Persistence Ignorance • No modifications to your classes!

Page 19: Entity Framework 4 In Microsoft Visual Studio 2010

N-Tier

Page 20: Entity Framework 4 In Microsoft Visual Studio 2010

20

Code First (was Code Only)

• Create the database from the classes– No model– Convention to config

Page 21: Entity Framework 4 In Microsoft Visual Studio 2010

21

Database First / Model First / CodeOnly

• Database First (EF v1)

• Model First

• Code First

Page 22: Entity Framework 4 In Microsoft Visual Studio 2010

Code Only

Page 23: Entity Framework 4 In Microsoft Visual Studio 2010

23

N-Tier (Self Tracking Entities)Servicepublic class NorthwindService : INorthwindService{ public Customer GetCustomer(string id) { using (var ctx = new NorthwindEntities()) { return ctx.Customers.Include("Orders") .Where(c => c.CustomerID == id) .SingleOrDefault(); } }

public bool SubmitOrder(Order newOrder) { using (var ctx = new NorthwindEntities()) { ctx.Orders.ApplyChanges(newOrder); ValidateOrderGraph(ctx, newOrder); return ctx.SaveChanges() > 0; } }}

Clientstatic bool ValidSTEUpdate(INorthwindService svc){ var customer = svc.GetCustomer("ALFKI");

// modify contact name of customer customer.ContactName += "+";

// add a new order var newOrder = new Order(); newOrder.Order_Details.Add(new Order_Detail() { ProductID = products.Where(p => p.ProductName == "Chai").Single().ProductID, Quantity = 1 }); customer.Orders.Add(newOrder);

return svc.SubmitOrder(newOrder);}

Danny Simmons MSDN Article: Building N-Tier Apps with EF4http://code.msdn.microsoft.com/mag200911EF4 http://msdn.microsoft.com/en-gb/magazine/ee335715.aspx

Page 24: Entity Framework 4 In Microsoft Visual Studio 2010

24

Entity Framework

• The designer!• No support for Plain Old CLR Objects• No Pluralization/Singularization• Foreign Keys hidden• Model First not supported• Lazy Loading not supported• Missing LINQ Operators vs LINQ to SQL• Generated SQL unreadable• N-Tier difficult• Poor support for Stored Procedures

Page 25: Entity Framework 4 In Microsoft Visual Studio 2010

25

Summary

• Entity Framework 4.0 is rather nice!• We continue to listen and improve• Resources– http://bit.ly/ericnelson – Entity Framework 4.0 Resources http://bit.ly/ef4resources – Entity Framework Team Blog– http://blogs.msdn.com/adonet/– Entity Framework Design Blog http://blogs.msdn.com/efdesign/

Page 26: Entity Framework 4 In Microsoft Visual Studio 2010

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