Entity Framework
Agenda
● ORM
● EF Modelling
● LINQ
● EF Inheritance
● EF Concurrency
Why Database Layer
Object Impedance Mismatch - RDBMS and OOP
Object Conversions - RDBMS to OOP and OOP to RDBMS
Needs Easy Conversion Process - Rise of ORM
ORM?
Many Frameworks - EF, NH, LINQ to SQL
RDBMS to NoSQL Support
Single RDBMS Support to Various RDBMS Support
DB Layer Uniformity Across the Organization
Entity Framework - Roadmap
EF - Key Terms
Entity
Context - Object Context, Db Context
Deferred Execution
POCO - Plain Old CLR Object
LINQ to Entities, Entity SQL
Mapping, Conceptual Model, Storage Model
Projections
EF - Resultant Objects
IEnumerable vs IQueryable
EF - Loading
Lazy, Eager Loading and Explicit Loading
Loading Navigation Properties
1. LazyLoadingEnabled = true
2. LazyLoadingEnabled = false & Include(“{Navigation Property}”)
3. LazyLoadingEnabled = false & {Navigation Property}.Load()
Modelling - .edmx
Modelling - .edmx - Types
Database First
Model First
Modelling - Database First
Reverse engineer model in EF Designer
Classes auto-generated from model
Preferred approach for existing database
Hands On
Database First - Tables, Stored Procedures, Views, Functions
Update Model
Modelling - Model First
Create model in EF Designer
Generate database from model
Classes auto-generated from model
Preferred approach for new database.
Hands On
Model First - Create Model, Generate Database
Update Model
Modelling - Code First
Define classes and mapping in code
Database created from code
Migrations apply model changes to database
Preferred approach for new database. Generally
preferred in Agile/Scrum
Modelling - Fluent Api, Configuration and Conventions
Fluent Api - Chaining Api
Configuration with Fluent Api
Conventions
Hands On
Code First - Create Classes with Conventions only
Database Generation
Database Migration
EF - CRUD
EF - Read Operation
EF - Create - Single Record
EF - Create - Multiple Records
EF - Update Record
EF - Delete Record
Homework
AddressBook - Design Db and Do CRUD operations on all entities
EF - LINQ
Filtering, Ordering
Joining
Grouping
EF - LINQ - Filtering, Ordering
EF - LINQ - Joining
EF - LINQ - Grouping
Homework
Full Outer Join Possible?
Use of “let” variable
EF - Code First
Conventions
Data Annotations
FluentApi
EF - Code First - Conventions
Convention: Table Name
Default: Entity + s (e.g Users)
Convention: Primary Key
Default: Id
Convention: Foreign Key Relation
Default: Entity + Id (e.g SubscriptionId)
EF - Code First - Data Annotations
Annotation: Table
=> Table(“{Name”})
Annotation: Key
=> Key
Annotation: ForeignKey
=> ForeignKey(“{Navigation Property”})
=> ForeignKey(“{Property”})
EF - Code First - Fluent Api
Homework
AddressBook - Design Db and Do CRUD operations on all entities
Executing Raw SQL - Plain, Stored Procedures
EF - Inheritance Strategy - TPH
EF - Inheritance Strategy - TPH
Needs a column, discriminator, that will used to identify exact type.
Retrieving Type in TPH
Code First Configuration
Gives better performance but there are many duplicate data and nullable columns.
EF - Inheritance Strategy - TPT
EF - Inheritance Strategy - TPT
Each entity maps to table. So CUD operations are faster
Code First Configuration
Retrieval is slower than TPH as it needs joins with base class/table.
EF - Inheritance Strategy - TPC
EF - Inheritance Strategy - TPC
Each concrete entity maps to table.
Code First Configuration
Properties of abstract entity are combined with concrete entity and many data/columns duplications.
EDMX designer does not support this mapping. Manual modification is required.
EF - Relationships
One to One
One to Many
Many to Many
EF - Relationships - One to One
EF - Relationships - One to Many
EF - Relationships - Many to Many
Hands On
One to One
One to Many
Many to Many
EF - Concurrency
Optimistic
Pessimistic
EF - Concurrency - Optimistic
None: Default and there is no concurrency
Fixed: Original value of concurrency column is included in where part while generating SQL
Rows are not locked. Read operation can be never locked
EF - Concurrency - Optimistic
Sample Code
EF - Concurrency - Pessimistic
Involves database locking operations. So it is slow
Use Transaction to achieve it
Read operation can be locked
Questions
Thank you
EF - Advance
Table Splitting
After little bit experience, it is worth
Bounded Contexts
After little bit experience, it is worth
EF - Unit Testing
After little bit experience, it is worth
EF - Patterns
After little bit experience, it is worth

Entity framework

  • 1.
  • 2.
    Agenda ● ORM ● EFModelling ● LINQ ● EF Inheritance ● EF Concurrency
  • 3.
    Why Database Layer ObjectImpedance Mismatch - RDBMS and OOP Object Conversions - RDBMS to OOP and OOP to RDBMS Needs Easy Conversion Process - Rise of ORM
  • 4.
    ORM? Many Frameworks -EF, NH, LINQ to SQL RDBMS to NoSQL Support Single RDBMS Support to Various RDBMS Support DB Layer Uniformity Across the Organization
  • 5.
  • 6.
    EF - KeyTerms Entity Context - Object Context, Db Context Deferred Execution POCO - Plain Old CLR Object LINQ to Entities, Entity SQL Mapping, Conceptual Model, Storage Model Projections
  • 7.
    EF - ResultantObjects IEnumerable vs IQueryable
  • 8.
    EF - Loading Lazy,Eager Loading and Explicit Loading Loading Navigation Properties 1. LazyLoadingEnabled = true 2. LazyLoadingEnabled = false & Include(“{Navigation Property}”) 3. LazyLoadingEnabled = false & {Navigation Property}.Load()
  • 9.
  • 10.
    Modelling - .edmx- Types Database First Model First
  • 11.
    Modelling - DatabaseFirst Reverse engineer model in EF Designer Classes auto-generated from model Preferred approach for existing database
  • 12.
    Hands On Database First- Tables, Stored Procedures, Views, Functions Update Model
  • 13.
    Modelling - ModelFirst Create model in EF Designer Generate database from model Classes auto-generated from model Preferred approach for new database.
  • 14.
    Hands On Model First- Create Model, Generate Database Update Model
  • 15.
    Modelling - CodeFirst Define classes and mapping in code Database created from code Migrations apply model changes to database Preferred approach for new database. Generally preferred in Agile/Scrum
  • 16.
    Modelling - FluentApi, Configuration and Conventions Fluent Api - Chaining Api Configuration with Fluent Api Conventions
  • 17.
    Hands On Code First- Create Classes with Conventions only Database Generation Database Migration
  • 18.
  • 19.
    EF - ReadOperation
  • 20.
    EF - Create- Single Record
  • 21.
    EF - Create- Multiple Records
  • 22.
  • 23.
  • 24.
    Homework AddressBook - DesignDb and Do CRUD operations on all entities
  • 25.
    EF - LINQ Filtering,Ordering Joining Grouping
  • 26.
    EF - LINQ- Filtering, Ordering
  • 27.
    EF - LINQ- Joining
  • 28.
    EF - LINQ- Grouping
  • 29.
    Homework Full Outer JoinPossible? Use of “let” variable
  • 30.
    EF - CodeFirst Conventions Data Annotations FluentApi
  • 31.
    EF - CodeFirst - Conventions Convention: Table Name Default: Entity + s (e.g Users) Convention: Primary Key Default: Id Convention: Foreign Key Relation Default: Entity + Id (e.g SubscriptionId)
  • 32.
    EF - CodeFirst - Data Annotations Annotation: Table => Table(“{Name”}) Annotation: Key => Key Annotation: ForeignKey => ForeignKey(“{Navigation Property”}) => ForeignKey(“{Property”})
  • 33.
    EF - CodeFirst - Fluent Api
  • 34.
    Homework AddressBook - DesignDb and Do CRUD operations on all entities Executing Raw SQL - Plain, Stored Procedures
  • 35.
    EF - InheritanceStrategy - TPH
  • 36.
    EF - InheritanceStrategy - TPH Needs a column, discriminator, that will used to identify exact type. Retrieving Type in TPH Code First Configuration Gives better performance but there are many duplicate data and nullable columns.
  • 37.
    EF - InheritanceStrategy - TPT
  • 38.
    EF - InheritanceStrategy - TPT Each entity maps to table. So CUD operations are faster Code First Configuration Retrieval is slower than TPH as it needs joins with base class/table.
  • 39.
    EF - InheritanceStrategy - TPC
  • 40.
    EF - InheritanceStrategy - TPC Each concrete entity maps to table. Code First Configuration Properties of abstract entity are combined with concrete entity and many data/columns duplications. EDMX designer does not support this mapping. Manual modification is required.
  • 41.
    EF - Relationships Oneto One One to Many Many to Many
  • 42.
    EF - Relationships- One to One
  • 43.
    EF - Relationships- One to Many
  • 44.
    EF - Relationships- Many to Many
  • 45.
    Hands On One toOne One to Many Many to Many
  • 46.
  • 47.
    EF - Concurrency- Optimistic None: Default and there is no concurrency Fixed: Original value of concurrency column is included in where part while generating SQL Rows are not locked. Read operation can be never locked
  • 48.
    EF - Concurrency- Optimistic Sample Code
  • 49.
    EF - Concurrency- Pessimistic Involves database locking operations. So it is slow Use Transaction to achieve it Read operation can be locked
  • 50.
  • 51.
  • 52.
  • 53.
    Table Splitting After littlebit experience, it is worth
  • 54.
    Bounded Contexts After littlebit experience, it is worth
  • 55.
    EF - UnitTesting After little bit experience, it is worth
  • 56.
    EF - Patterns Afterlittle bit experience, it is worth

Editor's Notes

  • #50 http://www.codeproject.com/Articles/114262/ways-of-doing-locking-in-NET-Pessimistic-and-opt