DCSIMG
Slide Decks and Demos from my ADO.Net Entity Framework Talk - Guy Burstein's Blog

Guy Burstein's Blog

Developer Evangelist @ Microsoft

News

Guy Burstein The Bu

Disclaimer
Postings are provided 'As Is' with no warranties and confer no rights.

Guy Burstein LinkedIn Profile

TwitterCounter for @bursteg

Slide Decks and Demos from my ADO.Net Entity Framework Talk

Yesterday I gave my first talk in front of the Israeli Architects User Group. I had put a lot of effort into this talk (creating the presentation, creating the demos and stuff), and it was all worth it. It seemed that people had a nice time, the atmosphere was great, and a few people came to me after the presentation to tell that is was great. I got back home very satisfied and looking forward to my next talk…

I will go over the main issues of the talk here in this post for those who couldn't come or for those who had forgotten something:

Motivation
We pay a really high price both in development time and maintenance for the was our relational database is designed. When we talk about the conceptual model of our business applications we use terms like entities,  inheritance and relations that cannot be naturally expressed in relational databases. So the way we make up for it causes our code (Stored Procedures, Database View or ad hoc queries in our code) to contain "deep knowledge" of the logical schema of the database.

Introducing ADO.Net Entity Framework
The next version of ADO.Net contains a new framework called ADO.Net Entity Framework that helps us to separate the way out application views the data from the way this data is represented in the database. The framework contains a new data model for design time and a run-time engine that supports this model. The framework uses a technology called Client Views which is a mapping technology that maps the conceptual schema (the way our application views the data) to the logical schema. What is important about this technology (and explains its name) is that it is implemented and executed on the client side. This gives each application a separate "view" of the data instead of polluting a central database with application-specific views or stored procedure which are harder to maintain.

Entity Data Model
In order to model the data at a higher level of abstraction we now use a new data model called Entity Data Model. This model is a new Entity Relationship Model that allows us to use all the terms we couldn’t use when we modeled data in the relational database. Key concepts that are used in this model are Entities, that are instances of Entity Types. EntityTypes are a structured record with a key. Entities are grouped into EntitySets.

Mapping Provider
After creating a model, we want to query it and get result in terms of the new model. So as a natural evolution of ADO.Net, we use the Provider Model. But instead using SqlCommand and SqlConnection, we can now use MapCommand and MapConnection instead.
With the Map Provider we can replace ADO.Net SQL Commands that targets the logical schema of the database such as this one:

using (SqlConnection conn = new SqlConnection(Settings.Default.AWv3SqlConnectionString))
{
    conn.Open();
    SqlCommand
cmd = conn.CreateCommand();
    cmd.CommandText =
@"
        SELECT HireDate, FirstName
        FROM SalesPerson as sp
        INNER JOIN Employee as e ON sp.SalesPersonID = e.EmployeeID
        INNER JOIN Contact as c ON e.EmployeeID = c.ContactID
        WHERE HireDate < @Date"
;
        cmd.Parameters.AddWithValue(
"Date"
, date);

    DbDataReader r = cmd.ExecuteReader(CommandBehavior
.SequentialAccess);
    while
(r.Read())
    {
        Console.WriteLine(" {0:d}\t{1}", r["HireDate"], r["FirstName"
]);
    }
}

with another command that targets the conceptual schema. (The conceptual schema had an entity type called SalesPerson that was mapped to Contact, Employee and SalesPerson tables in the logical schema).

using (MapConnection conn = new MapConnection(Settings.Default.AWv3MappingConnectionString))
{
    conn.Open();
    MapCommand
cmd = conn.CreateCommand();
    cmd.CommandText =
@"
        SELECT sp.HireDate, sp.FirstName    
        FROM AdventureWorksModel.AdventureWorks.SalesPeople as sp
        WHERE sp.HireDate < @Date"
;
    cmd.Parameters.AddWithValue(
"Date"
, date);

    DbDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
    while
(r.Read())
    {
        Console.WriteLine(" {0:d}\t{1}", r["HireDate"], r["FirstName"
]);
    }
}

Entity SQL
The motivation to create the new data model was to be able to use all the above terms that we couldn't use when working against the logical schema of the database. So in order to take advantage of those terms when we use the mapping provider, the SQL Statement we provide the MapCommand object is really a statement in a new query language called Entity SQL. This new language is a variation of SQL as we know, but let us query the model in terms of entities, relationships and inheritance. Since the Client Views run in the client side, the Entity SQL statement is processed and translated into actual SQL Statement on the client side. This behavior allows this query language to be store-independent. You can change the database you work against, but the Entity SQL statement stays the same.

Some examples to Entity SQL:

Selecting a full entity by using VALUE keyword:

 using (MapConnection conn = new MapConnection(Settings.Default.AWv3MappingConnectionString))
{
    conn.Open();
    MapCommand
cmd = conn.CreateCommand();
    cmd.CommandText =
@"
        SELECT VALUE sp    
        FROM AdventureWorksModel.AdventureWorks.SalesPeople as sp
        WHERE sp.HireDate < @Date"
;
    cmd.Parameters.AddWithValue(
"Date"
, date);

    DbDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
    while
(r.Read())
    {
        Console.WriteLine(" {0:d}\t{1}", r["HireDate"], r["FirstName"
]);
    }
}

Navigating thourgh relations using NAVIGATE operator.

 using (MapConnection conn = new MapConnection(Settings.Default.AWv3MappingConnectionString))
{
    conn.Open();
    MapCommand
cmd = conn.CreateCommand();
    cmd.CommandText =
@"
        SELECT VALUE sp    
        FROM AdventureWorksModel.AdventureWorks.SalesPeople as sp
        WHERE NAVIGATE(so, AdventureWorksModel.SalesPerson_Order).HireDate < @Date"
;
    cmd.Parameters.AddWithValue(
"Date"
, date);

    DbDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
    while
(r.Read())
    {
        Console.WriteLine(" {0:d}\t{1}", r["HireDate"], r["FirstName"
]);
    }
}

Getting entities according to thier type (using inheritance):

 using (MapConnection conn = new MapConnection(Settings.Default.AWv3MappingConnectionString))
{
    conn.Open();
    MapCommand
cmd = conn.CreateCommand();
    cmd.CommandText =
@"
        SELECT VALUE sp    
        FROM AdventureWorksModel.AdventureWorks.SalesPeople as sp
       
AND so IS OF(AdventureWorksModel.StoreSalesOrder);
    cmd.Parameters.AddWithValue(
"Date"
, date);

    DbDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
    while
(r.Read())
    {
       
IExtendedDataRecord er = (IExtendedDataRecord)r;
       
Console.Write(er.DataRecordInfo.Metadata.Name);
        Console.WriteLine(" {0:d}\t{1}", r["HireDate"], r["FirstName"
]);
    }
}

ADO.Net Object Layer
There is no doubt that using rows and columns is great for several scenarios such as Reporting or Business Intelligence. But, when you write heavy business logic against the results of your query, in most cases you would prefer working with objects.
Instead of building a new stack for Object Relational Mapping, the ADO.Net team has chosen to build a new object layer above the existing ADO.Net stack. As a matter of fact, your conceptual model should not be any different if you eventually want to get objects back or rows and columns.
While using the provider model on the mapping layer (using MapConnection and MapCommand), using the object layer is slightly different. This layer has a single entry point called ObjectContext. For example:

Using Object Context to query objects:

ObjectContext ctx = new ObjectContext(Settings.Default.AWv3MappingConnectionString, "AdventureWorksModel.AdventureWorks");

Query<SalesPerson> query = ctx.GetQuery<SalesPerson>(
@"
    SELECT VALUE sp
    FROM AdventureWorksModel.AdventureWorks.SalesPeople as sp
    WHERE sp.HireDate < @Date"
,
    new QueryParameter("Date"
, date));

foreach (SalesPerson sp in
query)
{
    Console.WriteLine(" {0:d}\t{1}"
, sp.HireDate, sp.FirstName);
}

Using Object Context to reflect changes back to the database:

ObjectContext ctx = new ObjectContext(Settings.Default.AWv3MappingConnectionString, "AdventureWorksModel.AdventureWorks");

Query<SalesPerson> query = ctx.GetQuery<SalesPerson>(
@"
    SELECT VALUE sp
    FROM AdventureWorksModel.AdventureWorks.SalesPeople as sp
    WHERE sp.HireDate < @Date"
,
    new QueryParameter("Date"
, date));

foreach (SalesPerson sp in
query)
{
 
   if (sp.FirstName == "Amy")
   
{
       
sp.Bonus += 10;
       
sp.MiddleName =
"Guy";
       
sp.Title =
"Senior Consultant";
    
}

    Console.WriteLine(" {0:d}\t{1}", sp.HireDate, sp.FirstName);
}
ctx.SaveChanges();

Language Integrated Query and LINQ to Entities
In order to eliminate the impedance mismatch between programming languages and the way we communicate with the database (usually SQL), we can use LINQ. LINQ is a set of innovations for programming languages that allows us to integrate queries as part of the programming language we use to write our business logic. Using LINQ we can use intellisence, compilation checks, and pass typed parameters to our queries.

An example for LINQ query ober the conceptual model:

AdventureWorks db = new AdventureWorks();

var query = from sp in
db.SalesPeople
where
sp.HireDate.Year == 2006
select
sp;

foreach (SalesPerson sp in query)
{
   
Console.WriteLine(" {0:d}\t{1}"
, sp.HireDate, sp.FirstName);
}

I hope you also enjoyed the presentation and demos. You can download the slide deck from here, and the demos from here.

Using the Demos:
Before you download and start playing with the demos, you should install:
LINQ May CTP 
ADO.Net vNext August CTP
Entity Data Model Designer CTP
After installing, read further instructions here.

If you have any questions regarding the presentation or demos, I'd like to here them. More important, if you have any feedback you want to provide about the ADO.Net Entity Framework, I'd like to hear about it!

Enjoy!

Comments

Nadav Israeli said:

Hi,

The Talk was excellent, and definitly worth hearing. it was clear you've put a lot of effort preparing it.

- the zip file that contains the demos can't be opened after downloading (at list for me) - can you please check it?

the most important feedback i can provide in regard to the object layer is the need to make db changes (update/insert) with objects that came thru another layer (usually after serialization) and were not retrieved in the current ObjectContext.

updating collection and many-to-many relation tables is another issue that most ORM tools address today.

Thanks again,

Nadav Israeli

nadavi@rafael.co.il

# October 23, 2006 7:13 PM

Vladimirv said:

Hi,

Great session. I liked both the presentation, the examples and the way you talked.

Hope to hear you again

Vladimir Vexler,

vladimir.vexler@gmail.com

# October 24, 2006 4:54 PM

Guy Burstein's Blog said:

I've been playing with ADO.Net Entity Framework for quite some time now. I have already given a talk

# January 19, 2007 12:28 PM

Guy Burstein's Blog said:

Last October, when I gave a talk about ADO.Net Entity Framework , one missing piece was to enable updating

# March 9, 2007 11:47 AM

Guy Burstein's Blog said:

This is my #200 post, and it is a good time to look back at what I've posted over the last few months.

# March 9, 2007 8:51 PM

Guy Burstein's Blog said:

According to ADO.Net Team announcement earlier today, the ADO.Net Entity Framework will not ship with

# April 29, 2007 2:46 PM

Guy Burstein's Blog said:

I've been following the ADO.Net Team Blog for quite some time now, sice the ADO.Net Entity Framework

# May 31, 2007 6:19 PM

Guy Burstein's Blog said:

ADO.Net Entity Framework Stored Procedures Last October, when I gave a talk about ADO.Net Entity Framework

# July 25, 2007 2:22 AM

Guy Burstein [MVP] said:

Entity Model Designer - What came out of it in Visual Studio 2008 Back in March, the Data Programmability

# October 26, 2007 11:29 AM

Guy Burstein [MVP] said:

Entity Data Model Designer - What came out of it in Visual Studio 2008 Back in March, the Data Programmability

# October 31, 2007 8:27 AM

Presentations - Guy Burstein [MVP] said:

Pingback from  Presentations - Guy Burstein [MVP]

# November 29, 2007 3:44 PM

ламинат said:

9zThank's.3o I compleatly agree with last post.  yxc

<a href="http://skuper.ru">ламинированный паркет</a> 9u

# August 24, 2008 9:51 AM

ламинат said:

7fThank's.9i I compleatly disagree with last post .  wjj

<a href="http://skuper.ru">паркетная доска</a> 7y

# August 24, 2008 5:21 PM

ламинат said:

8vThank's.0f I compleatly disagree with last post .  chs

<a href="http://skuper.ru">ламинированный паркет</a> 8c

# August 24, 2008 6:14 PM

ламинат said:

5lGood idea.6x I compleatly agree with last post.  ifi

<a href="http://skuper.ru">ламинированный паркет</a> 1h

# August 24, 2008 9:09 PM

сайдинг said:

1jI'll thingk about it.1u I compleatly agree with last post.

<a href="all-siding.ru/index.php сайдинг</a> 5i

<a href="all-siding.ru/index.php продажа</a> 1s

# August 26, 2008 10:50 AM

сайдинг said:

2nI'll thingk about it.3h I compleatly disagree with last post .

<a href="all-siding.ru/index.php сайдинг</a> 7k

<a href="all-siding.ru/index.php сайдинг</a> 5f

# August 26, 2008 2:39 PM

Model Yachts said:

Dynamic lighting: Notice that as you approach the stars, light is cast on the ship. As you change direction or move away the direction and intensity change. Filters on particles: As you move the ship around, lots of little stars are shot out as thrust

# April 21, 2009 8:06 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: