Entity Framework Migration


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Projecting: L2S can project to a structure. EF can't (run time exception) (see http://www.thedatafarm.com/blog/2008/08/15/AFewThingsYouCantDoWithEFQueriesWhichYouWontFindOutUntilRuntime.aspx)
  • Both frameworks have similar implementations for selecting collections of objects from stored procsFor POCO Objects – L2S can fetch using ExecuteCommand and will map the fields from the return type to the object type supplied. Missing or additional columns are ignored. EF doesn’t support POCO with V1.Multiple Results – L2S can handle multiple results but they must be coded manually. EF doesn’t have support for multiple results, but see the EFEXtensions library for a solution. Also see http://blogs.msdn.com/meek/archive/2008/03/26/ado-entity-framework-stored-procedure-customization.aspx. Scalar results – L2S can auto-map. EF requires manual mapping.Executing without results – L2S uses ExecuteCommand, EF requires creating ADO code as extension method of the context using ExecuteNonQuery
  • EF:Final projection must be an entity type that has the correct association and it can’t be modified along the way (Group By) see http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework
  • Entity Framework Migration

    1. 1. Moving from LINQ to SQL to EF<br />Jim Wooley<br />
    2. 2. Agenda<br />Why migrate?<br />Entity Shapes<br />Query differences<br />Context considerations<br />Demo migration<br />
    3. 3. Migration Implications<br />Migration not required <br />If it ain’t broke, why fix it?<br />Because it’s cool != cost justification<br />Why Migrate?<br />Increase maintainability<br />Reduce complexity<br />Add functionality<br />Incremental migration encouraged<br />Use it in new components<br />Have tests to ensure refactoring<br />
    4. 4. Entity shapes<br />
    5. 5. Physical<br />Entity mapping layers<br />Conceptual<br />Mapping<br />Logical<br />SSDL<br />MSL<br />CSDL<br />Author<br />Author<br />Author<br />Entity<br />User<br />Address<br />Publisher<br />Publisher<br />Entity<br />Publisher<br />
    6. 6. Many-to-Many<br />
    7. 7. Inheritance – Table per Heirarchy<br />
    8. 8. Inheritance-Table per Type<br />
    9. 9. Inheritance-Table per Class<br />
    10. 10. POCO Considerations<br />L2S<br />EF<br />Collections inherit from Table<T><br />POCO Entities Supported through XML Mapping<br />Collections from ObjectQuery<T><br />POCO Entities Inherit from EntityObject or ComplexObject<br />IPOCO Support<br />Coming with V4<br />Post 4.0: Code First/Code Only<br />
    11. 11. Mapping options<br />
    12. 12. Migrating an Application<br />Demo – Getting it to compile<br />
    13. 13. Query generation<br />Dim q1 = FromcustInnwd.Customers _<br />         Wherecust.Country.StartsWith(“U") <br />LINQ to SQL:WHERE Country LIKE @P0; @P0=“U%”<br />EF 1.0:<br /> Where(CAST(CHARINDEX(N’U%’, Country) AS int)) = 1<br />EF 4.0: WHERE Extent1.Country LIKE N’U%’<br />
    14. 14. Query differences<br />L2S<br />EF<br />Use Contains to create IN clause<br />Projecting to Structure<br />First/Single<br />No support for IN. Use multiple OR clauses in WHERE statement.<br />(Fixed in 4.0)<br />
    15. 15. Dynamic Queries<br />LINQ to SQL<br />EF<br />Use deferred execution and query compositionality<br />Dynamically build Expression Trees<br />IQueryable extended to take strings as predicates<br />Use Entity SQL<br />
    16. 16. Stored Procedures to Select<br />LINQ to SQL<br />Entity Framework<br />Select as Function<br />Ctx.ExecuteCommand for POCO results<br />Ctx.IMultipleResults<br />Scalar results<br />ExecuteCommand<br />Select as Function<br />No support for POCO<br />No support for Multiple Results (see EFExtensions)<br />Build extension method for scalar results<br />Extension method using ExecuteNonQuery<br />
    17. 17. Stored Procedures to Update<br />LINQ to SQL<br />EF<br />Use designer to map any/all stored procs<br />Infers usage based on the InsertT, UpdateT, DeleteT functions<br />Must map all 3 portions of CUD operation<br />
    18. 18. Dynamic Updates<br />L2S<br />EF<br />Ctx.P.InsertOnSubmit<br />Ctx.P.DeleteOnSubmit<br />Child – Add/Remove<br />InsertAllOnSubmit<br />Ctx.AddObject<br />Ctx.DeleteObject<br />Ctx.AddToT,<br />Ctx.DeleteFromT<br />Child – Add/Remove<br />
    19. 19. Context Considerations<br />Options<br />Concurrency<br />Transactions<br />Exceptions<br />Logging<br />
    20. 20. Eager Loading<br />LINQ to SQL<br />EF<br />DataLoadOptions<br />For grandchildren: include multiple LoadWith<br />Can’t change options once set on context<br />Can add filtering<br />Parent.Include(“Child”)<br />Parent.Include(“Child.GC”)<br />Options set per query<br />Filter in query<br />
    21. 21. Deferred (Lazy) Loading<br />LINQ to SQL<br />EF<br />Enabled as long as the context remains in scope<br />DeferredLoadingEnabled<br />Explicitly call Child.Load<br />CodeGallery sample for Lazy Loading.<br />Lazy Loading by default in 4.0<br />
    22. 22. Migrating an Application<br />Demo – Fixing the Bugs<br />
    23. 23. Important points<br />Migration isn’t required<br />Be prepared to justify cost<br />Have regression baselines before refactoring<br />Good architecture begets easier migrations<br />Benchmark and check performance<br />Check generated queries<br />
    24. 24. Resources<br />http://blogs.msdn.com/adonet/<br />http://blogs.msdn.com/efdesign/<br />EF Migration whitepaper:http://msdn.microsoft.com/en-us/library/cc716791.aspx<br />Forums: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/threads/<br />http://learnentityframework.com<br />
    25. 25. Jim Wooley<br />www.ThinqLinq.com<br />www.LinqInAction.net<br />www.Twitter.com/LinqKinq <br />Questions<br />