+ All Categories
Home > Education > Microsoft Entity Framework

Microsoft Entity Framework

Date post: 22-May-2015
Category:
Upload: mahmoud-tolba
View: 970 times
Download: 2 times
Share this document with a friend
Description:
Microsoft Entity framework is one of the best ORM tools that facilitates the data access.
Popular Tags:
20
Microsoft Entity Framework
Transcript
Page 1: Microsoft Entity Framework

MicrosoftEntity Framework

Page 2: Microsoft Entity Framework

Microsoft Entity Framework

• Microsoft Entity framework is one of the best object relational mapping frameworks.

• Facilitates accessing databases and other relational data sources rather than the other frameworks.

• The current version is EF 6.0 (see http://msdn.microsoft.com/en-us/data/jj574253.aspx)

Page 3: Microsoft Entity Framework

Object Relational Mapping (ORM)

It is a tool for storing data from domain objects to relational database like MS SQL Server in an automated way

without much programming. O/RM includes three main parts: Domain class objects, Relational database objects

and Mapping information on how domain objects maps to relational database objects (tables, views & stored

procedures). ORM helps us to keep our database design separate from our domain class design. This makes

application maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update &

Delete) so developer doesn’t need to write it manually

Page 5: Microsoft Entity Framework

ORM Tools (Cont.) Euss, open source Habanero, Free open source BATIS, Free open source Invist,free ORM and code generation tool LLBLGen, open source drivers, commercial LightSpeed, free or commercial Neo, open source NConstruct, commercial NHibernate, open source Opf3, free and commercial ObjectMapper .NET, GPL and commercial license OpenAccess, free or commercial TierDeveloper, free ORM and code generation tool Persistor.NET, free or commercial Quick Objects, free or commercial Sooda, open source; BSD license Subsonic, open source Orasis, free trial or buy. Telerik, express or buy. CSLA.NET, free. ECO, free or commercial nhydrate, open source .netTiers, open source

Page 6: Microsoft Entity Framework

Why Entity Framework

Domain model pattern, using this pattern means that you model your entities based on your business

concepts rather than the database layer. Provides mapping from logical layer to the physical layer.

Huge code reduction, allows developers to focus on the business logic and providing automated code

for storing and accessing the database

There is no SQL update, insert, delete statements written in different layers of the application.

Rich query capability, by providing rich object oriented query language.

Navigation. You can navigate object relationships transparently.

Concurrency, allowing multiple users updating the same data simultaneously.

Page 7: Microsoft Entity Framework

Entity Framework vs. Linq To SQL

Entity Framework LinQ to SQL (L2S)

Full provider model, supports DB2, Oracle, MySQL, SQL

Limited provider model

Model entities can map to one or more tables using table per type or table per inheritance

One entity only maps to a single table

Continuous support Microsoft is no longer supports it

Multiple modeling techniques Only single model technique

Page 8: Microsoft Entity Framework

Entity Framework Architecture

1. Entity Data Model2. Linq to Entities3. Entity SQL4. Object Service “The entry point for accessing database and returning data to it”5. Entity Client data provider “Responsible for converting L2E and Entity SQL queries to

database queries”6. ADO.NET data provider “this layer communicates directly with the database using standard ADO.NET”

Page 9: Microsoft Entity Framework

[email protected]

EF Code First

There are two new types introduced for Code First approach, DbContext and DbSet. DbContext is a

simplified alternative to ObjectContext and is the primary object for interacting with a database using a

specific model. DbSet(Of TEntity) is a simplified alternative to ObjectSet(Of TEntity) and is used to perform

CRUD operations against a specific type from the model in Code First approach.

• To include the entity framework in your project using nugget

Type get-package -remote -filter entityframework then type install-package -id EntityFramework

• use data annotations to specify properties at the target database.

 

Page 10: Microsoft Entity Framework

[email protected]

Code first to an existing database

Install entity framework power tools from visual studio gallery

http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d

Just right click the project and select reverse engineer Code First

Page 11: Microsoft Entity Framework

[email protected]

Performing CRUD operations

Page 12: Microsoft Entity Framework

[email protected]

Query1. Linq To Entities

2. Entity SQL

Page 13: Microsoft Entity Framework

[email protected]

Query

3. Native SQL//Querying with native sql using (SchoolDBEntities ctx = new SchoolDBEntities()) { //Inserting Student

using ExecuteStoreCommand

int InsertedRows = ctx.ExecuteStoreCommand("Insert into Student(StudentName,StandardId)

values('StudentName1',1)");

//Fetching student using ExecuteStoreQuery

var student = ctx.ExecuteStoreQuery<Student>("Select * from Student where StudentName = 'StudentName1'",

null).ToList(); }

Page 14: Microsoft Entity Framework

[email protected]

Update entities

Page 15: Microsoft Entity Framework

[email protected]

Self Tracking Entities

There is common problem when using entity framework with multitier application, entities

lose states outside the scope of an object context,

Solution: Self tracking entities

Page 16: Microsoft Entity Framework

[email protected]

Add new entities

Page 17: Microsoft Entity Framework

[email protected]

Delete Entities

Page 18: Microsoft Entity Framework

[email protected]

Concurrency

A concurrency conflict occurs when one user displays an entity's data in order to edit it, and then another

user updates the same entity's data before the first user's change is written to the database. If you don't

enable the detection of such conflicts, whoever updates the database last overwrites the other user's

changes.

There are two types of concurrency

1. Pessimistic Concurrency (Locking)

2. Optimistic Concurrency

Page 19: Microsoft Entity Framework

[email protected]

Transactions

We use transactions when multiple calls of SaveChanges are called or when multiple operations are

performed on a single entity object.

There are two approaches for performing Transactions

1. DbTransaction

2. TransactionScope

Page 20: Microsoft Entity Framework

[email protected]

EF performance Tuning

1. Efficiently load related data

2. Use NoTracking merge option

3. Pre-Compile linQ queries

4. Examine query commands sent to the database.

1. Efficiently load related data

2. Use NoTracking merge option

3. Pre-Compile linQ queries

private static readonly Func<SchoolEntities, IQueryable<InstructorName>>

compiledInstructorNamesQuery = CompiledQuery.Compile((SchoolEntities context) => from i

in context.InstructorNames orderby i.FullName select i);


Recommended