Download - Entity Framework
Entity Framework
Agenda
● ORM● EF Modelling● LINQ● EF Inheritance● EF Concurrency
Why Database Layer
Object Impedance Mismatch - RDBMS and OOPObject 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 SupportSingle RDBMS Support to Various RDBMS Support
DB Layer Uniformity Across the Organization
Entity Framework - Roadmap
EF - Key Terms
EntityContext - Object Context, Db Context
Deferred Execution
POCO - Plain Old CLR Object
LINQ to Entities, Entity SQL
Mapping, Conceptual Model, Storage ModelProjections
EF - Resultant Objects
IEnumerable vs IQueryable
EF - Loading
Lazy, Eager Loading and Explicit Loading
Loading Navigation Properties1. LazyLoadingEnabled = true2. 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 DesignerClasses auto-generated from model
Preferred approach for existing database
Hands On
Database First - Tables, Stored Procedures, Views, FunctionsUpdate Model
Modelling - Model First
Create model in EF DesignerGenerate database from modelClasses auto-generated from model
Preferred approach for new database.
Hands On
Model First - Create Model, Generate DatabaseUpdate Model
Modelling - Code First
Define classes and mapping in codeDatabase created from codeMigrations apply model changes to databasePreferred 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 onlyDatabase GenerationDatabase 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, OrderingJoiningGrouping
EF - LINQ - Filtering, Ordering
EF - LINQ - Joining
EF - LINQ - Grouping
Homework
Full Outer Join Possible?Use of “let” variable
EF - Code First
ConventionsData AnnotationsFluentApi
EF - Code First - Conventions
Convention: Table NameDefault: Entity + s (e.g Users)
Convention: Primary KeyDefault: Id
Convention: Foreign Key RelationDefault: 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 entitiesExecuting 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 ConfigurationGives 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 ConfigurationRetrieval 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 ConfigurationProperties 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 OneOne to ManyMany to Many
EF - Relationships - One to One
EF - Relationships - One to Many
EF - Relationships - Many to Many
Hands On
One to OneOne to ManyMany to Many
EF - Concurrency
OptimisticPessimistic
EF - Concurrency - Optimistic
None: Default and there is no concurrencyFixed: Original value of concurrency column is included in where part while generating SQLRows are not locked. Read operation can be never locked
EF - Concurrency - Optimistic
Sample Code
EF - Concurrency - Pessimistic
Involves database locking operations. So it is slowUse Transaction to achieve itRead 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