Entity Framework Migration

25
MOVING FROM LINQ TO SQL TO EF Jim Wooley

Transcript of Entity Framework Migration

Page 1: Entity Framework Migration

MOVING FROM LINQ TO SQL TO EF

Jim Wooley

Page 2: Entity Framework Migration

AGENDA

Why migrate?

Entity Shapes

Query differences

Context considerations

Demo migration

Page 3: Entity Framework Migration

MIGRATION IMPLICATIONS

Migration not required

If it ain’t broke, why fix it?

Because it’s cool != cost justification

Why Migrate?

Increase maintainability

Reduce complexity

Add functionality

Incremental migration encouraged

Use it in new components

Have tests to ensure refactoring

Page 4: Entity Framework Migration

ENTITY SHAPES

Page 5: Entity Framework Migration

ENTITY MAPPING LAYERS

Physical

Logical

SSDL

Mapping

MSL

Conceptual

CSDL

Author

Publisher

Author

Address

Publisher

Author

Entity

Publisher

Entity

User

Page 6: Entity Framework Migration

MANY-TO-MANY

Page 7: Entity Framework Migration

INHERITANCE – TABLE PER HEIRARCHY

Page 8: Entity Framework Migration

INHERITANCE-TABLE PER TYPE

Page 9: Entity Framework Migration

INHERITANCE-TABLE PER CLASS

Page 10: Entity Framework Migration

POCO CONSIDERATIONS

L2S EF

Collections inherit from

Table<T>

POCO Entities Supported

through XML Mapping

Collections from ObjectQuery<T>

POCO Entities Inherit from EntityObject or ComplexObject

IPOCO Support

Coming with V4

Post 4.0: Code First/Code Only

Page 11: Entity Framework Migration

MAPPING OPTIONS

LINQ to SQL

EF

Page 12: Entity Framework Migration

Migrating an Application

DEMO – GETTING IT TO COMPILE

Page 13: Entity Framework Migration

QUERY GENERATION

Dim q1 = From cust In nwd.Customers _

Where cust.Country.StartsWith(“U")

LINQ to SQL:WHERE Country LIKE @P0; @P0=“U%”

EF 1.0:

Where(CAST(CHARINDEX(N’U%’, Country) AS int)) = 1

EF 4.0:WHERE Extent1.Country LIKE N’U%’

Page 14: Entity Framework Migration

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)

Page 15: Entity Framework Migration

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

Page 16: Entity Framework Migration

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

Page 17: Entity Framework Migration

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

Page 18: Entity Framework Migration

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

Page 19: Entity Framework Migration

CONTEXT CONSIDERATIONS

Options

Concurrency

Transactions

Exceptions

Logging

Page 20: Entity Framework Migration

EAGER LOADING

LINQ TO SQL EF

DataLoadOptions

For grandchildren: include

multiple LoadWith

Can’t change options once

set on context

Can add filtering

Parent.Include(“Child”)

Parent.Include(“Child.GC”)

Options set per query

Filter in query

Page 21: Entity Framework Migration

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

Page 22: Entity Framework Migration

Migrating an Application

DEMO – FIXING THE BUGS

Page 23: Entity Framework Migration

IMPORTANT POINTS

Migration isn’t required

Be prepared to justify cost

Have regression baselines before refactoring

Good architecture begets easier migrations

Benchmark and check performance

Check generated queries

Page 25: Entity Framework Migration

Jim Wooley

www.ThinqLinq.com

www.LinqInAction.net

www.Twitter.com/LinqKinq

QUESTIONS