21
ADO. Net Entity Framework 4.0

Entity framework 4.0

Embed Size (px)

DESCRIPTION

ADO.NET Entity framework demonstration by Brij Bhusan for Kolkatageeks.com.

Citation preview

Page 1: Entity framework 4.0

ADO. Net Entity Framework 4.0

Page 2: Entity framework 4.0

Need of ORM tools EDM Creation  and Components of EDM

◦ Database First Approach◦ Model First Approach

Working with Linq to Entities Using Stored Procedure with EDM Customizing Entities with T4 Templates Creating & using POCO entities

Page 3: Entity framework 4.0

Why do we need ORM Tools?

Page 4: Entity framework 4.0

using (SQLConnection conn = new SQLConnection(“<conn string>”); {

conn.Open();SQLCommand cmd = conn.CreateCommand();cmd.CommandText = “sp_StoredProc”;cmd.parameters.AddWithValue(“@City”, “Dallas”);using (SQLDataReader rdr = cmd.ExecuteReader()){

while (rdr.read()){ string name = rdr.GetString(0); string city = rdr.GetString(1);}

}}

Powerful, but fragile and time-consuming

4

Strings! No compile time check or Intellisense

Parameters loosely bound: --Names, types, number ofnot checked until runtimeResults are loosely typed

Page 5: Entity framework 4.0

Objects != Relational Data– OO been around for decades; relational databases even longer– Bridging the gap between has been time consuming and

expensive:• Legacy ADO.NET• Hand-built DALs• 3rd party frameworks

– But, the core problem remains!

Bigger Problem

5

Relational Data Objects

Page 6: Entity framework 4.0

So, Now…

6

Application

Customer.cs

Order.cs

Database

dbo.customer

dbo.order

Mapping CodeTransform OO to DB

We build plumbing to move data back and forth

from data store to business objects.

Instead of building business functionality,

Page 7: Entity framework 4.0

Reduce object-relational impedance mismatch

In built support of many Microsoft’s Product Programming Against a Model, Not the

Database Client side Data Model Unified Model to query against different Database Model - First Development POCO Support EDM Designer Rapid Development

Page 8: Entity framework 4.0

• Meta Data• EDM design tools• Database first design• Model first design• Code generation (T4)• Object Services • POCO support• Change tracking• Relationship management• Entity Client• Lazy loading• N –tier application support

Page 9: Entity framework 4.0

Bridge between application and data store Provide ability to work with a conceptual view of data

rather than the actual database schema NET APIs provided by the Entity Framework use an

EDM for every interaction with the data store, whether it is to retrieve or to save data

The Entity Framework tools generate classes from this model that enable you to work with objects described by the EDM

Page 10: Entity framework 4.0

Demo EDM Creation (Database First approach)

Page 11: Entity framework 4.0

EDM Designer Components◦ Scalar Properties◦ Navigation Properties◦ Associations◦ Entity Container, Entity(Entity Sets) and Attributes

properties◦ Multiplicity

• 1 (One) • * (Many) • 0..1 (Zero or One)

Page 12: Entity framework 4.0
Page 13: Entity framework 4.0

EDM Components◦ Conceptual Model◦ Storage Model◦ Mappings

Page 14: Entity framework 4.0

The mapping layer sits between the conceptual and store layers

Provides the map from the entity properties back to the tables and columns in the data store

Enables further customization of the model

Page 15: Entity framework 4.0

Demo EDM Creation (Model First approach)

Page 16: Entity framework 4.0

Projection – Selecting a specific properties or expression in a query.

Eager Loading – Requesting the related data to be loaded along with the query result.

Defferred Loading – Delaying the loading of the requesting data along with the query result.

Navigating – Moving from an entity to its related entity data.

Page 17: Entity framework 4.0

Navigations in Queries –Related data can loaded, navigated easily with the help of the relationship in EDM. Not require to construct joins.

Navigating to an entity reference Filtering and Sorting with an Entity Reference Navigating to the Entity collection Filtering and Sorting with EntityCollections Aggregates with EntityCollections Joining tables Grouping Aggregating and filtering the grouped data Loading the related data

◦ Eager loading◦ Lazy loading

Page 18: Entity framework 4.0

Managing the entity state ObjectContext -ObjectstateEntry (Unchanged,

modified, addedd or deleted)

EntityObject – EntityState – The entity state should be in Sync with objectStateEntry state since this entity managed by objectContext.

Saving change back to DB- ObjectContext has a single method, saveChanges, which persists back to the database all of the changes made to the entities.

Adding, Updating and Deleting an Entity

Page 19: Entity framework 4.0

Stored Procedures returning data can be linked to entities, Scalar Values or Complex types

Mapping Insert, Update and Delete functions.

Mapping Read Stored Procedures◦ To Entity◦ To Scalar◦ To Complex Types

Page 20: Entity framework 4.0

POCO : Plain old CLR Objects Provides the ability to add custom classes

in EDM Demo

Page 21: Entity framework 4.0

Thank you