Entity Framework Code First Beyond the Basics Sergey
Entity Framework Code First – Beyond the Basics Sergey Barskiy, Magenic Microsoft MVP – Data Platform Magenic, Principal Consultant Level: Introductory / Intermediate
Entity Framework Code First • Entity Framework in Microsoft’s ORM Tool • Has been out since 2008 • Code First has been out since 2011
Performance • Understanding of Caching Context Metadata and query commands • View Generation • Precompiled Views (4. x vs. 5. 0) Limit Complexity of views Limit data Limit scope of context (using()) Lazy Loading pitfalls • •
Performance • CRUD Efficiencies – Update – Delete – Insert – Select (No Tracking)
Db. Entity. Entry • State • Current, Original, Database values • Complex. Property, Collection, Reference Entity Reload Get. Validation. Result Access to see if a scalar property was modified (requires all properties to be virtual) • •
Organizing the Model • Hierarchies • Table per hierarchy • Types in an inheritance hierarchy are mapped to a single table. A condition clause is used to define the entity types • Table per type • Types are all mapped to individual tables. Properties that belong solely to a base type or derived type are stored in a table that maps to that type • Table per concrete type • Non-abstract types are each mapped to an individual table. Each of these tables must have columns that map to all of the properties of the derived type, including the properties inherited from the base type
Organizing the Model • Complex Types • A complex type is a non-scalar property of an entity type that does not have a key property. A complex type can contain other nested complex types. • Entity Splitting • Properties from a single entity in the conceptual model are mapped to columns in two or more underlying tables. • Excluding Types and Properties • Properties and/or classes are excluded from storage. • Explicit Mapping of Types and Propeties
More… • Relationships • All are supported • One-to-one needs explicit configuration • • Cascading deletes Working with Views and Stored Procedures
More… • Create your own repository • Create/Update/Delete • Select • http: //efcodefirstextras. codeplex. com/ • • Concurrency handling Conventions – Creating your own conventions
Handling Schema Changes • Code First Migrations • Create database • Maintain schema • Command line migrations • Custom Migrations • Using 3 rd Party tooling
Code First Migrations • Enable automatic migrations • Update-Database – Has to be run to create database • Add-Migration – Add manual migration – Up / Down methods – Supplying default values for non-nullable columns • Migrate. Database. To. Latest. Version<T, C>
Custom Migrations • Use third-party tooling – Quest – Red Gate – Etc… • Write Custom Initializer
RDBMS Providers • Available providers • • • Dev. Art Data. Direct Oracle My. SQL. etc… Mileage may vary
Links • http: //msdn. microsoft. com/enus/library/cc 853327. aspx (Performance)
Contact Info • Sergey. B@Magenic. com • www. dotnetspeak. com
- Slides: 15