HV-Lima Peru

Offshore your .NET/SQL Development to Latin America! Languages: en-de-es

Monthly Archives: April 2010

How to call a Stored Procedure through EF4?

In the following it is used the AdventureWorks DB on SQL Server 2008.

Add a new Stored Procedure from the DB to the Entity Framework via Update Model:

In the case I added the following usp:

EF has two parts, the DB-side (where we added the Stored Proc) and the object-side where we will create the needed classes now.
A double click on the new Stored Proc above (or right click, Add Function Import) opens the dialog Add Function Import below.


Click the first button (A:) to get the output column info from the SP, then the second button (B:) to create a new complex type, based on this info, then OK

This creates the complex type and C#-Function on the EF4-object-side:

Calling the SP through EF is accomplished like so:
Observe the datatype of the list p

Here comes the result of the first entry of p:

Compare this with the result of calling directly the SP in SSMS:

Example for Deferred Execution in Linq

Both examples use the AdventureWorks DB on SQL Server 2008 and the Entity Framework 4.0

1. Deferred Execution

In the first case the query is armed in line 23, but not materialized.

In line 27 result is “Bothell” (see watch on the right side)
Then there is done an update in line 32.
In line 35 the result is Berlin.

This shows clearly, that the execution of the query is deferred to the moment of enlisting.

2. NOT deferred Execution

In this case in the formulation of the query the result is materialized by calling the extension .ToList() in line 43.

Result in line 47 gives back “Bothell” as like Result2 in line 55, despite of having changed the address to Berlin in the meantime!