entity framework

of 56 /56
Entity Framework

Author: vrluckyin

Post on 13-Apr-2017

187 views

Category:

Software


0 download

Embed Size (px)

TRANSCRIPT

Entity Framework

AgendaORMEF ModellingLINQEF InheritanceEF Concurrency

Why Database LayerObject 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 SQLRDBMS to NoSQL SupportSingle RDBMS Support to Various RDBMS Support DB Layer Uniformity Across the Organization

Entity Framework - Roadmap

EF - Key TermsEntityContext - Object Context, Db ContextDeferred ExecutionPOCO - Plain Old CLR ObjectLINQ to Entities, Entity SQLMapping, Conceptual Model, Storage ModelProjections

EF - Resultant ObjectsIEnumerable vs IQueryable

EF - LoadingLazy, 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 - TypesDatabase First

Model First

Modelling - Database First

Reverse engineer model in EF DesignerClasses auto-generated from model

Preferred approach for existing database

Hands OnDatabase 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 OnModel First - Create Model, Generate DatabaseUpdate Model

Modelling - Code First

Define classes and mapping in codeDatabase created from codeMigrations apply model changes to database

Preferred approach for new database. Generally preferred in Agile/Scrum

Modelling - Fluent Api, Configuration and ConventionsFluent Api - Chaining Api

Configuration with Fluent Api

Conventions

Hands OnCode 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

HomeworkAddressBook - Design Db and Do CRUD operations on all entities

EF - LINQFiltering, Ordering

Joining

Grouping

EF - LINQ - Filtering, Ordering

EF - LINQ - Joining

EF - LINQ - Grouping

HomeworkFull Outer Join Possible?

Use of let variable

EF - Code FirstConventions

Data Annotations

FluentApi

Vipul patel () - http://www.entityframeworktutorial.net/code-first/inheritance-strategy-in-code-first.aspxVipul patel () - http://msdn.microsoft.com/en-us/data/jj618292Vipul patel () - http://msdn.microsoft.com/en-us/data/jj618293EF - Code First - ConventionsConvention: Table NameDefault: Entity + s (e.g Users)

Convention: Primary KeyDefault: Id

Convention: Foreign Key RelationDefault: Entity + Id (e.g SubscriptionId)

EF - Code First - Data AnnotationsAnnotation: Table=> Table({Name})

Annotation: Key=> Key

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

EF - Code First - Fluent Api

HomeworkAddressBook - Design Db and Do CRUD operations on all entities

Executing Raw SQL - Plain, Stored Procedures

EF - Inheritance Strategy - TPH

EF - Inheritance Strategy - TPHNeeds 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 - TPTEach 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 - TPCEach 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 - RelationshipsOne to One

One to ManyMany to Many

EF - Relationships - One to One

EF - Relationships - One to Many

EF - Relationships - Many to Many

Hands OnOne to One

One to Many

Many to Many

EF - ConcurrencyOptimistic

Pessimistic

EF - Concurrency - OptimisticNone: Default and there is no concurrency

Fixed: 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 slow

Use Transaction to achieve itRead operation can be locked

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

Questions

Thank you

EF - Advance

Table SplittingAfter little bit experience, it is worth

Bounded ContextsAfter little bit experience, it is worth

EF - Unit TestingAfter little bit experience, it is worth

EF - PatternsAfter little bit experience, it is worth