Download - Entity Framework

Transcript
Page 1: Entity Framework

Entity Framework

Page 2: Entity Framework

Agenda

● ORM● EF Modelling● LINQ● EF Inheritance● EF Concurrency

Page 3: Entity Framework

Why Database Layer

Object Impedance Mismatch - RDBMS and OOPObject Conversions - RDBMS to OOP and OOP to RDBMS

Needs Easy Conversion Process - Rise of ORM

Page 4: Entity Framework

ORM?

Many Frameworks - EF, NH, LINQ to SQL

RDBMS to NoSQL SupportSingle RDBMS Support to Various RDBMS Support

DB Layer Uniformity Across the Organization

Page 5: Entity Framework

Entity Framework - Roadmap

Page 6: Entity Framework

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

Page 7: Entity Framework

EF - Resultant Objects

IEnumerable vs IQueryable

Page 8: Entity Framework

EF - Loading

Lazy, Eager Loading and Explicit Loading

Loading Navigation Properties1. LazyLoadingEnabled = true2. LazyLoadingEnabled = false & Include(“{Navigation Property}”) 3. LazyLoadingEnabled = false & {Navigation Property}.Load()

Page 9: Entity Framework

Modelling - .edmx

Page 10: Entity Framework

Modelling - .edmx - Types

Database First

Model First

Page 11: Entity Framework

Modelling - Database First

Reverse engineer model in EF DesignerClasses auto-generated from model

Preferred approach for existing database

Page 12: Entity Framework

Hands On

Database First - Tables, Stored Procedures, Views, FunctionsUpdate Model

Page 13: Entity Framework

Modelling - Model First

Create model in EF DesignerGenerate database from modelClasses auto-generated from model

Preferred approach for new database.

Page 14: Entity Framework

Hands On

Model First - Create Model, Generate DatabaseUpdate Model

Page 15: Entity Framework

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

Page 16: Entity Framework

Modelling - Fluent Api, Configuration and Conventions

Fluent Api - Chaining Api

Configuration with Fluent Api

Conventions

Page 17: Entity Framework

Hands On

Code First - Create Classes with Conventions onlyDatabase GenerationDatabase Migration

Page 18: Entity Framework

EF - CRUD

Page 19: Entity Framework

EF - Read Operation

Page 20: Entity Framework

EF - Create - Single Record

Page 21: Entity Framework

EF - Create - Multiple Records

Page 22: Entity Framework

EF - Update Record

Page 23: Entity Framework

EF - Delete Record

Page 24: Entity Framework

Homework

AddressBook - Design Db and Do CRUD operations on all entities

Page 25: Entity Framework

EF - LINQ

Filtering, OrderingJoiningGrouping

Page 26: Entity Framework

EF - LINQ - Filtering, Ordering

Page 27: Entity Framework

EF - LINQ - Joining

Page 28: Entity Framework

EF - LINQ - Grouping

Page 29: Entity Framework

Homework

Full Outer Join Possible?Use of “let” variable

Page 30: Entity Framework

EF - Code First

ConventionsData AnnotationsFluentApi

Vipul patel
http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspx
Vipul patel
http://msdn.microsoft.com/en-us/data/jj618292
Vipul patel
http://msdn.microsoft.com/en-us/data/jj618293
Page 31: Entity Framework

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)

Page 32: Entity Framework

EF - Code First - Data Annotations

Annotation: Table=> Table(“{Name”})

Annotation: Key=> Key

Annotation: ForeignKey=> ForeignKey(“{Navigation Property”})=> ForeignKey(“{Property”})

Page 33: Entity Framework

EF - Code First - Fluent Api

Page 34: Entity Framework

Homework

AddressBook - Design Db and Do CRUD operations on all entitiesExecuting Raw SQL - Plain, Stored Procedures

Page 35: Entity Framework

EF - Inheritance Strategy - TPH

Page 36: Entity Framework

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.

Page 37: Entity Framework

EF - Inheritance Strategy - TPT

Page 38: Entity Framework

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.

Page 39: Entity Framework

EF - Inheritance Strategy - TPC

Page 40: Entity Framework

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.

Page 41: Entity Framework

EF - Relationships

One to OneOne to ManyMany to Many

Page 42: Entity Framework

EF - Relationships - One to One

Page 43: Entity Framework

EF - Relationships - One to Many

Page 44: Entity Framework

EF - Relationships - Many to Many

Page 45: Entity Framework

Hands On

One to OneOne to ManyMany to Many

Page 46: Entity Framework

EF - Concurrency

OptimisticPessimistic

Page 47: Entity Framework

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

Page 48: Entity Framework

EF - Concurrency - Optimistic

Sample Code

Page 49: Entity Framework

EF - Concurrency - Pessimistic

Involves database locking operations. So it is slowUse Transaction to achieve itRead operation can be locked

Page 50: Entity Framework

Questions

Page 51: Entity Framework

Thank you

Page 52: Entity Framework

EF - Advance

Page 53: Entity Framework

Table Splitting

After little bit experience, it is worth

Page 54: Entity Framework

Bounded Contexts

After little bit experience, it is worth

Page 55: Entity Framework

EF - Unit Testing

After little bit experience, it is worth

Page 56: Entity Framework

EF - Patterns

After little bit experience, it is worth


Top Related