entity framework 5 - code first

24
DEV223 Wekoslav Stefanovski Senior Developer Seavus Access your data the right way: Entity Framework 5 Code First

Upload: wekoslav-stefanovski

Post on 25-May-2015

1.852 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Entity Framework 5 - Code First

DEV223

Wekoslav StefanovskiSenior DeveloperSeavus

Access your data the right way: Entity Framework 5 Code First

Page 2: Entity Framework 5 - Code First
Page 3: Entity Framework 5 - Code First

Ве молиме исклучете ги вашите мобилни

уреди!

Page 4: Entity Framework 5 - Code First

Agenda

Why are ORM’s great?Why is Entity Framework great?When is Code First best choice for the developers?Complex Hierarchy Scenarios

Table Per HierarchyTable Per TypeTable Per Base Class

Entity Framework Migrations

Page 5: Entity Framework 5 - Code First

Who Am I

Wekoslav StefanovskiSenior DeveloperSeavus

C# (ab)user since 2001SQL Server user and administrator since

1998Member of the Macedonian .NET User

GroupCo-leader of the Macedonian Visual C#

User Group

Page 6: Entity Framework 5 - Code First

Why are ORM’s great?

Very controversial topicSome downsides of ORM’s

PerformanceLoss of fine-grained controlWorks magically, when it works (leaky abstraction)

Greatly reduces the time needed to develop the data access codeEnables us to think in object terms, and not in tables and fields

Page 7: Entity Framework 5 - Code First

When are ORM’s great?

Model Complexity

Th

rou

gh

pu

t

Complex Intranet Applications

Public web application

Simple local applications

Twitter - like

Page 8: Entity Framework 5 - Code First

Why is Entity Framework great?

Three models of data accessModel firstDatabase firstCode first

Usable directly from c# codeGreat LINQ supportGreat mapping API

Page 9: Entity Framework 5 - Code First

Why is Entity Framework 5 great?

Enum support (finally)Automatic migrations

Beware: Entity Framework 5 works only on .NET 4.5

Page 10: Entity Framework 5 - Code First

Model / Database first

Model First: We generate the model and that will generate the database (localDb support)Database First: We use a preexisting database to build the modelSound like opposites, but are essentially the sameUses t4 templates to generate the model classesFast and easy to setup initially

Page 11: Entity Framework 5 - Code First

Model / Database first

Can be non-obvious to map complex relationshipsThe model classes are not ours, they belong to the model now.

Page 12: Entity Framework 5 - Code First

demo

Model and Database First

Entity Framework 5

Page 13: Entity Framework 5 - Code First

Code First

We create the databaseWe create the model classesEntity Framework just maps themMakes us feel like we’re in controlCan be relatively complex to set-upFluid API makes the configuration easierEnables us to work the may we like

Page 14: Entity Framework 5 - Code First

demo

Code First Example

Entity Framework 5

Page 15: Entity Framework 5 - Code First

Select n+1 problem

Common problem in ORMsHappens when a lazy developer uses lazy loadingCan be disastrous to performance by an order of magnitudeEntity Framework include parameters

Page 16: Entity Framework 5 - Code First

Complex Hierarchy Scenarios

Sometimes the Object and the Relationship part don’t really map that wellOften occurs in polymorphic and hierarchical scenarios.Table Per Hierarchy ScenarioTable Per Type ScenarioTable Per Base Class Scenario

Page 17: Entity Framework 5 - Code First

Table Per Hierarchy

We have a single table that hosts multiple model classesPotentially lots of null columns“Discriminator” column

Page 18: Entity Framework 5 - Code First

Table Per Type

Each model class is contained in a different tableTable(s) for the base class(es)“Discriminator” column

Page 19: Entity Framework 5 - Code First

Table Per Base Class

Common properties that exist in multiple model classes

Page 20: Entity Framework 5 - Code First

demo

Complex Hierarchy Scenarios

Entity Framework 5

Page 21: Entity Framework 5 - Code First

Entity Framework 5 Migrations

Must be enabled separatelyWork somewhat magicallyAutomatic Migrations

Update database on first accessWill not allow data loss conversions (unless we say so)Great way to “invisibly” bring the database up to dateNot really useful if the development and production database are different.

Manual migrations generate migration scripts

Page 22: Entity Framework 5 - Code First

discussion

Any questions?

Q&A

Page 23: Entity Framework 5 - Code First
Page 24: Entity Framework 5 - Code First