Applying EF Code First at Your Job
                 Gabriel ENEA
                  MAXCODE
     gabriel.enea@maxcode.ro, @dotnet18
               26 November 2011
     CodeCamp de toamnă la Iași, România
Agenda

             EF Intro




                           EF
     Demos
                         Mapping




             EF Fluent
                API
ORM, O/RM, and O/R mapping




Objects   Mapping   Data
Entity Framework
Entity Framework 4.0
  part of .NET 4.0 / VS 2010
Entity Framework 4.1 / 4.2
  Code First support
Getting/Installing via Nuget package
Entity Framework Components


                     Object
                  Services API



    In-      Entity Data
  memory       Model
  Metadata



                   Entity Client
                       API
EF <= v4.0 - EDMX Mapping
EF >= v4.1 -Code First
   Mapping
Data Annotations

                   Fluent API
Code-First development
EF Development Approaches
               Database First
   Existing                     Generated Data
  Database                      Model (.edmx)

                Model First
  Data Model                      Generated
   (.edmx)                        Database

                Code First
  Data Model                      Generated
   (classes)                      Database

                Today’s Demos
   Existing                     Generated Data
  Database                      Model (classes)
Entity Framework Context
ObjectContext
DbContext (Code First support)

Roles:
  tracking changes and managing
   identities, concurrency, and relationships.
DbContext
• Locating entities with key value
• Add/Remove/Attach/Query
• Modify State
• Change Tracking API (current/original values)
• Built-in Model-Level Validation and
  DataAnnotations support
• Raw SQL Query/Command Support
Demo 1
Northwind database
Convention over Configuration
Mappings
Convention over Configuration
                 Products
                   table

                            One-to-many /
                             Foreign Key



                            Entity Key /
                            Primary Key
Mappings - Fluent API
Create metadata at runtime
Advanced mapping configurations
Domain classes free of infrastructure code
 (DataAnnotations attributes, EntityObject)
Rules defined within DbContext class
 (OnModelCreating event)
Better support for existing databases
Demo 2
Northwind database
Fluent API
  Change Primary Key name
  Mapping a Complex Type
Fluent API – Primary Keys
Fluent API – Properties
Fluent API - Types
Demo 3
Fluent API - Relationships
  Configure One-to-Many & Many-to-Many
Fluent API - Relationships
Demo 4
Fluent API
  Configure Table Splitting
Fluent API – Table/Entity
splitting
Fluent API
Inheritance Table Mapping
/* default mapping strategy */
TPH (Table per Hierarchy)
Fluent API
Inheritance Table Mapping
TPT (Table per Type)
Fluent API
Inheritance Table Mapping
TPC (Table per Concrete Type)
Demo 5 (timeout!?)
Northwind database
Create Employee
Data validation
Fluent API Mappings
Key field(s)
Relationships
Attributes
Complex types
Inheritance
Table/Column metadata
Table Splitting
Entity Splitting
Summary
EF overview
EF 4.2 with CodeFirst and DbContext
Fluent API in action
Demos, EF CodeFirst with existing
 database
Resources
EF 4.2 MSDN Library
  http://msdn.microsoft.com/en-
  us/library/gg696172(v=VS.103).aspx


Book: Programming Entity Framework: Code
 First, Julia Lerman, Rowan Miller

Blogs:
  Entity Framework Team:
   http://blogs.msdn.com/adonet
  Morteza Manavi
   http://weblogs.asp.net/manavi
Q&A



Applying EF Code First at Your Job
Please fill the evaluation form

  Thank you very much!
               Gabriel ENEA
                MAXCODE
   gabriel.enea@maxcode.ro, @dotnet18
             26 November 2011
   CodeCamp de toamnă la Iași, România

Applying EF Code First at Your Job