entity framework migration

of 25/25
MOVING FROM LINQ TO SQL TO EF Jim Wooley

Post on 07-Jul-2015

1.778 views

Category:

Technology

0 download

Embed Size (px)

TRANSCRIPT

  • 1. Moving from LINQ to SQL to EF
    Jim Wooley

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