You are on page 1of 66

Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.

Bnei Brak 51202 Israel


www.sela.co.il

2009 Gil Fink. All rights reserved.

Entity Framework
Introduction
Exploring the Entity
Data Model
Querying and
Manipulating Entity
Data Models
Customizing Entity
Data Models Examples
EF4
Summary

2009 Gil Fink. All rights reserved.

The Impedance Mismatch

Relational
Database

2009 Gil Fink. All rights reserved.

Conceptual /
Business Model
(Objects)

Data access framework


Supports data-centric applications and
services
Enables programming against a conceptual
application model
Enables independency of any data storage
engine or relational schema

2009 Gil Fink. All rights reserved.

EF uses a model called an Entity Data Model


(EDM)
EDM is a client-side data model
EDM is an abstraction layer on top of the
data storage
Remove the pain of
Interacting with the data storage
Translating the data into objects

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Items described in the EDM are called


entities
Entities have only properties but no behavior
Entities can have relationships with other
entities

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

The model doesn't have any knowledge of the


data storage
The backend data storage has no impact on
your model or your code
Uses a provider model to interact with the data
storage
Available providers:
SQL Server
Oracle
MySQL
Many more
2009 Gil Fink. All rights reserved.

Entity Framework
Introduction
Exploring the Entity
Data Model
Querying and
Manipulating Entity
Data Models
Customizing Entity
Data Models Examples
EF4
Summary

2009 Gil Fink. All rights reserved.

Automatically generates classes from the


model
Takes care of all of the database connectivity
Provides common query syntax for querying
the model
Provides a mechanism for tracking changes
to the model's objects

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Designer Window:
Graphical representation of an EDM and its
members
Enables adding more features to the model
Enables properties configuration
Enables updating from the data store
Enables model validation

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

The Three Parts of the Model:

The image is taken from Julia Lermans book Programming Entity


Framework, 1st Edition

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

EF automatically creates a set of classes


from the model
You work with the model through the
generated classes
Every change to the model can change the
generated classes

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Entity Framework
Introduction
Exploring the Entity
Data Model
Querying and
Manipulating Entity
Data Models
Customizing Entity
Data Models Examples
EF4
Summary

2009 Gil Fink. All rights reserved.

Queries are built against a data model


EDM query transform into data storage query
Query results materialize into model entities

The image is
taken from
Julia Lermans
book
Programming
Entity
Framework,
1st Edition

2009 Gil Fink. All rights reserved.

Three kinds of queries in EF


LINQ to Entities
Entity SQL with Object Services
Entity SQL with Entity Client

Every kind has its pros and cons

2009 Gil Fink. All rights reserved.

Queries written in LINQ syntax


Support for LINQ features
Full IntelliSense support

var courses = from course in context.Courses


where course.Title.StartsWith("C")
orderby course.Title ascending
select new
}
Title = course.Title,
Location = course.Location
};

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

T-SQL-like query language


Provide the necessary capabilities for
querying the EDM
EF translates Entity SQL into storage-specific
queries
var qStr = @"SELECT VALUE c
FROM SchoolEntities.Courses AS c
WHERE c.Title=Calculus'";
var courses = context.CreateQuery<Course>(qStr);

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Streams data back to the application


Resembles SqlClient, OracleClient and the
other client providers
using (var conn = new EntityConnection("name=ProgrammingEFDB1Entities"))
{
conn.Open();
var qStr = "SELECT VALUE c FROM SchoolEntities.Courses AS c ";
var cmd = conn.CreateCommand();
cmd.CommandText = qStr;
using (var rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (rdr.Read())
{
Console.WriteLine(rdr.GetString(1));
}
}
}

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

LINQ to
Entities

Entity SQL
with Object
Services

Entity SQL
withEntity
Client

LINQ support

IntelliSense

Model dynamic
queries

Return type

Objects

Objects or
DbDataRecords

DbDataReader

Performance

2009 Gil Fink. All rights reserved.

Choose Entity SQL withEntity Client:


You want to return streamed data
You want to use EDM in existing applications

Choose Entity SQL with Object Services:


You want to express queries that LINQ doesnt
enable
You want dynamic queries
You want the best performance

Otherwise choose LINQ to Entities

2009 Gil Fink. All rights reserved.

Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel
www.sela.co.il

2009 Gil Fink. All rights reserved.

Every entity in ObjectContext has a


ObjectStateEntry
ObjectContext uses ObjectStateEntries to
track entity changes

2009 Gil Fink. All rights reserved.

Persists back to the data storage all the


changes made to entities
Uses the ObjectStateEntries to build the
relevant data storage commands
Unit of work pattern
context.SaveChanges();
OR
// doesnt refresh the entities state after save
context.SaveChanges(false);

2009 Gil Fink. All rights reserved.

Create entity in memory


Create in memory manually
Use one of the entitys generated create
methods

var department = new Department();


var course = Course.CreateCourse();

2009 Gil Fink. All rights reserved.

Add entity
By Assignment to an existing entitys property
By adding to a EntityCollection using Add method
Use ObjectContext AddTo methods

course.Department = department;
department.Courses.Add(course);
context.AddToCourses(course);

2009 Gil Fink. All rights reserved.

Change a property or reference to entity


The changes will be tracked by the
ObjectStateEntry
A call to SaveChanges will create update
command

2009 Gil Fink. All rights reserved.

Entity must be in hand in order to perform


delete
Use the ObjectContext DeleteObject method

// course need to be at hand


context.DeleteObject(course);

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Map procedures directly to ObjectContext as


methods
Sproc /
Method
Function
Map procedures to entities
Override automatically CUD behavior

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Entity Framework
Introduction
Exploring the Entity
Data Model
Querying and
Manipulating Entity
Data Models
Customizing Entity
Data Models Examples
EF4
Summary

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

The inheritance tree is create through one


table only
TPH inheritance depends on conditional
mapping
The condition is used to define records as
different types

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Are elements of the SSDL


Are created whenever you map a view into
the EDM
Are a projection of data and therefore are
read only
Using stored procedures you can add write
functionality
You can create your own DefiningQueries

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Entity Framework
Introduction
Exploring the Entity
Data Model
Querying and
Manipulating Entity
Data Models
Customizing Entity
Data Models Examples
EF4
Summary

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Database
First (v1)

Model
First (v4)

Entity Data
Model

Entity Data
Model

2009 Gil Fink. All rights reserved.

VS.

1) 1:1 DB Mapping
2) Could get crowded
3) No designer support
on V1

2009 Gil Fink. All rights reserved.

1) Flexibility from DB shape


2) Nicely organized
3) Designer supported

2009 Gil Fink. All rights reserved.

Class Definition
Everything
Navigation Property Definition
Scalar Property Definition

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

Get Product

2) Access
Category

Get Category

3) Get Category

2) Access
Category

2009 Gil Fink. All rights reserved.

Get Product
Get Category

Implicit

1) Get Product

Explicit

1) Get Product

In theory this
This
is perfect!
looks
really
good

In theory this also


looks really good

2009 Gil Fink. All rights reserved.

Entity SQL
Block

Umm, Id rather be using LINQ

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

1) Get Product
Web Service

2) Update Product
2) Make
Changes
Combines power of:
1)DataSet
2)DTOs

2009 Gil Fink. All rights reserved.

Entity Framework
Introduction
Exploring the Entity
Data Model
Querying and
Manipulating Entity
Data Models
Customizing Entity
Data Models Examples
EF4
Summary

2009 Gil Fink. All rights reserved.

Entity Framework brings massive changes to


data access area
We only scratched the surface
Entity Framework
Data access framework
Supports data-centric applications and services
Enables programming against a conceptual
application model
Has many features to be discovered
2009 Gil Fink. All rights reserved.

My Blog:
http://blogs.microsoft.co.il/blogs/gilf

ADO.NET Team Blog


http://blogs.msdn.com/adonet

Data Developer Center:


http://msdn.microsoft.com/data

2009 Gil Fink. All rights reserved.

2009 Gil Fink. All rights reserved.

You might also like