entity framework 5 - code first
TRANSCRIPT
DEV223
Wekoslav StefanovskiSenior DeveloperSeavus
Access your data the right way: 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
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
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
When are ORM’s great?
Model Complexity
Th
rou
gh
pu
t
Complex Intranet Applications
Public web application
Simple local applications
Twitter - like
Why is Entity Framework great?
Three models of data accessModel firstDatabase firstCode first
Usable directly from c# codeGreat LINQ supportGreat mapping API
Why is Entity Framework 5 great?
Enum support (finally)Automatic migrations
Beware: Entity Framework 5 works only on .NET 4.5
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
Model / Database first
Can be non-obvious to map complex relationshipsThe model classes are not ours, they belong to the model now.
demo
Model and Database First
Entity Framework 5
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
demo
Code First Example
Entity Framework 5
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
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
Table Per Hierarchy
We have a single table that hosts multiple model classesPotentially lots of null columns“Discriminator” column
Table Per Type
Each model class is contained in a different tableTable(s) for the base class(es)“Discriminator” column
Table Per Base Class
Common properties that exist in multiple model classes
demo
Complex Hierarchy Scenarios
Entity Framework 5
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
discussion
Any questions?
Q&A