Eric Nelson
Developer & Platform Group
Microsoft Ltd
eric.nelson@microsoft.com
http://geekswithblogs.net/IUpdateable
http:...
ORMs
  Swift look at history and future of ORM on
  Windows
Entity Framework – with demos
  Entity Data Model
  Entity Ser...
Accessing data in 1990
• ODBC, embedded SQL


   Accessing data in 2000
   • ADO, Stored Procedures


       Accessing dat...
What is it?
   an Abstraction
   technique for working with relational tables as if they
   were objects in memory
   Inte...
Many ORMs out there
     LLBLGen Pro http://www.llblgen.com/
     Nhibernate http://www.hibernate.org/343.html
     Entity...
Typed Datasets (cough) – shipped 
   ObjectSpaces “v1” – never shipped 
   ObjectSpaces “v2” – never shipped 
   Micros...
Entity Framework and LINQ to Entities is our strategic
technology
   Entity Framework v2 in VS2010 and .NET Framework 4.0
...
{ Simple Walkthough }
LINQ to SQL       LINQ to Entities
Database Support    SQL Server        SQL Server, DB2,
                                ...
Entity Framework is not just about ORM
Entity Framework v1 is being adopted
   Typically on applications expected to “live...
“EF is still cutting
development time
                       “the entity framework
 in half. Ya gotta
                    ...
Tools and services to create an Entity
Data Model (EDM)
Tools and services for consuming an
Entity Data Model
Entity Data Model
Application model
   Mapped to a persistence
   store
                                Conceptual
Compris...
{ A look at mapping }
2      1               The ORM
                           - optional
         Object Services
3


        Entity Client
Familiar ADO.NET object model:
  EntityCommand
  EntityConnection
  EntityDataReader
  EntityParameter
  EntityTransaction...
Queries materialized as Objects
   ObjectContext
   ObjectQuery<T>
Built on top of Entity Client
Two query options:
   Ent...
{ Consuming an EDM}
ORM is an abstraction layer over data
 • Productivity
 • Database Independence
Microsoft has two ORMs
 • LINQ to SQL – onl...
For Everyone
     Pluralization
     Foreign Keys
     Stored Procedures
     Self tracking Entities
 For the Model Centri...
Pluralization
  Used in Database First and Model First
  Implementation
    Public abstract PluralizationService
    Defau...
Foreign Keys
  Independent Association – v1
    Product p = new Product
    {
         ID = 1,
          Name = quot;Bovri...
Stored Procedures
  Stored Procedures as functions
    Mapping support for stored procedure result
    Complex type as ret...
Entities do their own change tracking
   regardless of which tier changes are made on
Somewhere between DTOs and DataSets
Model First
   Start with a blank model
   Add entities/relationships (CSDL)
   Click “Create Database from
   Model”
    ...
Complex Types
  Map a new type to many types
  Adds designer support
Model Level using ESQL
<Function Name=“CustomerFullName” ReturnType=“String”>
    <Parameter Name=“customer” Type=“MyModel...
In V1 we had EntityClassGenerator which
could be configured using events
  Hard (it used CodeDom)
  Inflexible (not much c...
Persistence Ignorance
  Support for Plain Old CLR Objects
  No requirement to have specific interface, base
  class
Lazy L...
Code Only
  No XML files, no model!
  Convention to config
    E.g. Convention: xxxID to a primary
    key, schema=dbo
   ...
Improved N-Tier API
More LINQ query operators
More SQL generation optimizations
Inline functions in ESQL
Readonly mapping
1990 to   • Data Access technology remained
            reasonably static
2008 -    • Procedural, API access
          • S...
http://blogs.msdn.com/efdesign
  Entity Framework v2

http://geekswithblogs.net/IUpdateable
  Slides

Sessions Thursday on...
© 2008 Microsoft Ltd. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registe...
Driven by Reporting Services team + others
Query Tree Writing with no objects...
   LINQ query without objects...
   DbExp...
Entity Framework v1 and v2
Entity Framework v1 and v2
Entity Framework v1 and v2
Upcoming SlideShare
Loading in …5
×

Entity Framework v1 and v2

3,896 views

Published on

Overview of Entity Framework plus V2 overview of the new features in .NET 4.0

Published in: Technology
  • Be the first to comment

Entity Framework v1 and v2

  1. 1. Eric Nelson Developer & Platform Group Microsoft Ltd eric.nelson@microsoft.com http://geekswithblogs.net/IUpdateable http://twitter.com/ericnel
  2. 2. ORMs Swift look at history and future of ORM on Windows Entity Framework – with demos Entity Data Model Entity Services Object Services Entity Framework v2 – with no demos Overview of POCO, Model First etc
  3. 3. Accessing data in 1990 • ODBC, embedded SQL Accessing data in 2000 • ADO, Stored Procedures Accessing data in 2005 • ADO.NET, Datasets, DataReaders Accessing data in 2010 • ORM baby!
  4. 4. What is it? an Abstraction technique for working with relational tables as if they were objects in memory Intention is to hide away the complexity of the underlying tables and give a uniform way of working with data Why use it? Developer productivity Retain database independence Note: Using an ORM does not mean you must use the ORM!
  5. 5. Many ORMs out there LLBLGen Pro http://www.llblgen.com/ Nhibernate http://www.hibernate.org/343.html EntitySpaces http://www.entityspaces.net/Portal/Default.aspx Open Access http://www.telerik.com/products/orm.aspx DevForce http://www.ideablade.com/ XPO http://www.devexpress.com/Products/NET/ORM/ Lightspeed http://www.mindscape.co.nz/products/LightSpeed/default.aspx Plus many, many more No clear “winner” = relatively little adoption of ORM Developers waiting on Microsoft Of 31 .NET ORMs in 2003, 9 lasted to 2008
  6. 6. Typed Datasets (cough) – shipped  ObjectSpaces “v1” – never shipped  ObjectSpaces “v2” – never shipped  Microsoft Business Framework – never shipped  WinFS – never shipped  LINQ to SQL - shipped November 2007  Visual Studio 2008 & .NET Framework 3.5 LINQ to Entities - shipped August 2008  Visual Studio 2008 SP1 & .NET Framework 3.5 SP1 Note: LINQ to Entities is the most visible part of the ADO.NET Entity Framework
  7. 7. Entity Framework and LINQ to Entities is our strategic technology Entity Framework v2 in VS2010 and .NET Framework 4.0 Best of LINQ to SQL moves into LINQ to Entities Microsoft is using it Data Services - shipping Reporting Services Microsoft “M” Partners supporting it Database Vendors – IBM,OpenLink, DataDirect, Devart etc ORM vendors supporting it DevForce now target Entity Framework, replacing their own LLBLGen v3 will target Entity Framework as well as their own Devart Entity Developer It is not just about ORM
  8. 8. { Simple Walkthough }
  9. 9. LINQ to SQL LINQ to Entities Database Support SQL Server SQL Server, DB2, Oracle, Sybase, MySQL ... Object Relational Simple Complex Mapping Capabilities Not strategic  Status Strategic Often  Annoying Rarely
  10. 10. Entity Framework is not just about ORM Entity Framework v1 is being adopted Typically on applications expected to “live” a long time Entity Framework is only .NET 3.5 SP1 and above Entity Framework is probably not the best choice for a “short lived” SQL Server applications Entity Framework v1 has “warts” Designer It is annoying – buggy, clunky Exposes subset of the functionality Does not support model first N-tier story Stored Procedure Support Foreign Keys PoCo Guids SQL 2008 New Types
  11. 11. “EF is still cutting development time “the entity framework in half. Ya gotta can kiss my damn love it.” ass - this shit is ridiculous”
  12. 12. Tools and services to create an Entity Data Model (EDM) Tools and services for consuming an Entity Data Model
  13. 13. Entity Data Model Application model Mapped to a persistence store Conceptual Comprised of three layers: Conceptual (CSDL) Mapping (MSL) Mapping Storage (SSDL) Database agnostic Comprised of: Storage Entities Associations Functions
  14. 14. { A look at mapping }
  15. 15. 2 1 The ORM - optional Object Services 3 Entity Client
  16. 16. Familiar ADO.NET object model: EntityCommand EntityConnection EntityDataReader EntityParameter EntityTransaction Text-based results Read-only Uses Entity SQL
  17. 17. Queries materialized as Objects ObjectContext ObjectQuery<T> Built on top of Entity Client Two query options: Entity SQL LINQ Runtime services: Unit of work Identity tracking Eager/explicit loading
  18. 18. { Consuming an EDM}
  19. 19. ORM is an abstraction layer over data • Productivity • Database Independence Microsoft has two ORMs • LINQ to SQL – only SQL Server • Entity Framework (includes LINQ to Entities) – many RDBMS, strategic ADO.NET Entity Framework includes all the other bits  • EDM, Linq to Entities, ESQL, Object Services, Entity Services • Part of .NET Framework 3.5 SP1 Entity Data Model – mapping from conceptual to store • CSDL = Conceptual Schema Definition Language • SSDL = Store Schema Definition Language • MSDL = Mapping Schema Definition Language Two query languages • ESQL = Entity SQL. Read only query language similar to SQL • LINQ to Entities = Language Integrated Query to the EDM
  20. 20. For Everyone Pluralization Foreign Keys Stored Procedures Self tracking Entities For the Model Centric Developer Model First Complex Types Model Defined Functions For the Control Freak Code Generation For the Agilist Persistence Ignorance, Lazy Loading, Code Only + “Other stuff” Warning – not final. Expect changes!
  21. 21. Pluralization Used in Database First and Model First Implementation Public abstract PluralizationService Default implementation is English Only Default rules: EntityTypes / ComplexTypes are singularized EntitySets are pluralized Navigation Properties based on cardinality
  22. 22. Foreign Keys Independent Association – v1 Product p = new Product { ID = 1, Name = quot;Bovrilquot;, Category = context.Categories .Single(c => c.Name == quot;Foodquot;) }; context.SaveChanges(); FK Association – v2 Product p = new Product { ID = 1, Name = quot;Bovrilquot;, CategoryID = 13 }; context.Products.AddObject(p); context.SaveChanges();
  23. 23. Stored Procedures Stored Procedures as functions Mapping support for stored procedure result Complex type as return type Scalar and void return type Entity CUD using Stored Procedures No need to have SPs for all CUD
  24. 24. Entities do their own change tracking regardless of which tier changes are made on Somewhere between DTOs and DataSets
  25. 25. Model First Start with a blank model Add entities/relationships (CSDL) Click “Create Database from Model” Infer Storage Model (SSDL) Infer Mapping (MSL) Infer DDL Deploy DDL Modify and re-apply. Two way. But - deletes your data!!!
  26. 26. Complex Types Map a new type to many types Adds designer support
  27. 27. Model Level using ESQL <Function Name=“CustomerFullName” ReturnType=“String”> <Parameter Name=“customer” Type=“MyModel.customer”> <DefiningExpression> customer.FirstName + ‘ ‘ + customer.LastName </DefiningExpression> </Function> Enables from c in ctx.Customers select c.FullName() CLR Level [EdmFunction(quot;MyModelquot;, quot;CustomerFullNamequot;)] public static string FullName(this customer c) { return String.Format(quot;{0} {1}quot;, c.FirstName, c.LastName); } Enables Console.WriteLine(c.FullName());
  28. 28. In V1 we had EntityClassGenerator which could be configured using events Hard (it used CodeDom) Inflexible (not much control) In V2 we will have TemplatedEntityClassGenerator and will ship default T4 templates Customization via Tools Uses Workflow Foundation
  29. 29. Persistence Ignorance Support for Plain Old CLR Objects No requirement to have specific interface, base class Lazy Loading Can not do order.order_details.Load() But Can do lazy load Context.deferredloading=true
  30. 30. Code Only No XML files, no model! Convention to config E.g. Convention: xxxID to a primary key, schema=dbo E.g. Config: On map to schema sys and class property to table column [TableMapping(Schema=“sys”,TableName=“MyDBTable”] [ColumnMapping(PropertyName=“MyClassProp1”, Column Name=“table_column1”)] [Key(PropertyName=“MyClassProp1”)] public objectset<MyThing> MyThings...
  31. 31. Improved N-Tier API More LINQ query operators More SQL generation optimizations Inline functions in ESQL Readonly mapping
  32. 32. 1990 to • Data Access technology remained reasonably static 2008 - • Procedural, API access • Surfaced the database schema Tables • No clear ORM choice • LINQ – excellent addition to the .NET 2009 – languages • Entity Framework and the EDM – strategic Objects investment • Productivity leap
  33. 33. http://blogs.msdn.com/efdesign Entity Framework v2 http://geekswithblogs.net/IUpdateable Slides Sessions Thursday on SQL Data Services 9:30am Windows Azure 2:00pm
  34. 34. © 2008 Microsoft Ltd. 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.
  35. 35. Driven by Reporting Services team + others Query Tree Writing with no objects... LINQ query without objects... DbExpression expression=ctx.EntitySet(quot;Ordersquot;).Scan().Where(...); DbDataReader reader = ctx.ExecuteQuery(expression); Or from c in ctx.EntitySet(“Orders”).Scan() Select c.Property(“ShipCity”) Readonly mapping <EntityContainerMapping CdmEntityContainer=quot;CContainerquot; StorageEntityContainer=quot;SContainerquot; GenerateUpdateViews=quot;falsequot; >

×