Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Microsoft Entity Framework version 4.0 is a brand new ORM(Object Relational Mapper) from Microsoft. It’s provides now some new features which are not there in the earlier version of Entity framework. Let’s walk through a simple example of a new features which will create a new Entity class based on stored procedure result. We will use same table for this example for which they have used earlier for Linq Binding with Custom Entity.

Below is the table which have simple fields like first name,last name etc.SQL Table for Entity Example.

Let’s insert some data like following.

Sample data for Entity Framework Example

Below is the stored procedure which I am going to use for this example which will simply fetch data from the table.

CREATE PROCEDURE dbo.GetAllUsers

AS
SET NOCOUNT ON
SELECT
UserId,
UserName,
FirstName,
LastName,
FirstName + ' ' + LastName AS [FullName]

FROM dbo.Users

 

 

Now let’s create Entity model class just like below via Project->Add New Item->Go to Data tab and then select ADO.NET Entity Data Model.

Adding a New Enity model class for Entity Framework example

Once you click add a dialog box will appear which will ask for Data Model Contents there are two options Generate From Database and another one is Empty Model. Generate from database will create a Entity model from the ready made database while Empty model enable us to create a model first and then it will allow us to create a database from our model. We are going to use Generate From database for this example. Select Generate From Database like following and click Next.

Generating Model from database for Entity Framework 4.0 Example

After click on the next it will ask for database connection string like following where you need to apply connection string for that. I am already having connection string in my web.config so i just selected like below otherwise you need to create one via clicking on new connection. Also you need to specify the connection string name in web.config so it will put a connection string in connection string section with that name.

MyConnection

Once you click next it will fetch the all database objects information like Tables,Views and Stored Procedure like following. Here our purpose is to work with stored procedure so I just selected the stored procedure and selected the GetAllUsers Stored Procedure.

Selecting Stored procedure

After that it will create a Entity Model class in solution explorer.Now we want to bind the stored procedure with result class so first we need to create function which will call ‘GetAllUser’ stored procedure. To create function we just need to select Our Entity Model class and then go to Model Browser and select the Stored Procedure and right click->Add Function Import like following image.

Importing a function from stored procedure example.

It will start a new wizard and will go to next step like following image which will have four options 1. None 2. Scalars 3. Complex 4. Entities and there will be a button Get Column information once you click it. It will gather all the column information of stored procedure result set and then click ‘Create New Complex Type’ It will create a new complex type from the gathered column information.

Gathering a column info and then

You can also rename that class so I have renamed the class as ‘User Info’ like following.UserInfo

Now click ok and now it will create function which call the stored procedure and will return Object Result set of Type ‘UserInfo’ which we have just created. Now let’s bind that to a simple grid view to see how its works. So Let’s take a simple grid view like below.

<asp:GridView ID="grdUserList" runat="server">
</asp:GridView>

Now Let’s bind that grid view like following from the code behind file of asp.net like following.

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (BlogEntities myEntity = new BlogEntities())
{
grdUserList.DataSource = myEntity.GetAllUsers();
grdUserList.DataBind();
}
}

}

Just run it with Ctrl + F5 and below it the output in browser.Output of Entity Framework 4.0 Example

That’s it very easy and simple to bind complex type with stored procedure using ADO.NET Entity Framework 4.0. Hope this will help you.. Happy Programming!!!

Technorati Tags: ,,
Shout it
Published Wednesday, August 18, 2010 2:39 AM by Jalpesh P. Vadgama

Comments

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Wednesday, August 18, 2010 1:58 AM by Jinal

Hi,

1. What to do if stored procedure return dynamic columns as per parameter pass to the procedure.

2. What to do if stored procedure return multiple resultsets.

Thanks,

Jinal Patel

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Wednesday, August 18, 2010 6:15 AM by Jalpesh P. Vadgama

@Jinal

1. For that I will search and will let you know.

2.

EF team has extensions which will allow multiple result set.For more information see following.

code.msdn.microsoft.com/EFExtensions

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Friday, August 27, 2010 8:16 AM by GvR

But what if I want sorting and paging on the grid?

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Friday, August 27, 2010 1:43 PM by Ed

Hello,

I have the same issue as Jinal, for the project I'm working on, the stored proc will return some fixed columns but also n additional columns based on the passed parameters. Were you able to find any infomation about the possibility of using that within the EF?

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Friday, August 27, 2010 3:10 PM by Jalpesh P. Vadgama

@Jinal,@Ed,

Currently they are not supporting dynamic columns so either you have to do other way like normal ado.net or you need to change your sp to have static SP.

@Gvr,

It will if you use entity datasource.

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Wednesday, September 08, 2010 1:36 AM by Thyagaraju Govardhan

Thanks.

Thyagaraju Govardhan

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Tuesday, November 23, 2010 8:20 PM by Rich N

Why do my SPROC's not show up when in the Wizard?

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Tuesday, November 23, 2010 8:22 PM by rich_at_delta

My SPROC's don't show up in the Wizard - why's that?

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Friday, November 26, 2010 1:09 AM by Jalpesh P. Vadgama

@Rich,

did you selected stored procedure checkbox at the time of model creation. If yes then it should display.

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Thursday, March 10, 2011 7:35 AM by Deepak

Regarding "EF team has extensions which will allow multiple result set.For more information see following.

code.msdn.microsoft.com/EFExtensions", I can not find anywhere, do we have to create the entities manually and then use Materialize

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Friday, April 01, 2011 9:45 AM by weblogs.asp.net

Entity framework 4 0 bind stored procedure with result entity class.. I like it :)

# Object Oriented Programing &raquo; Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Pingback from  Object Oriented Programing &raquo; Entity Framework 4.0- Bind Stored Procedure with Result Entity class

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Friday, June 03, 2011 12:05 PM by Veronica

Very helpful. Thank you.

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Saturday, August 13, 2011 7:07 PM by James

I have the same type of functionality in my application bur the live of me I can't figure out how to get the min value of one of the fields? With your example how you would find the min userid in the entity?

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Monday, August 15, 2011 2:06 AM by Jalpesh P. Vadgama

@James

You can bind anything to stored procedure. So stored procedure can return the min userID.

Best Regards,

Jalpesh

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Monday, April 02, 2012 3:33 PM by lyban

yes it is simple. now try to sort by clicking columns to see how simple it is.

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Wednesday, July 11, 2012 9:37 AM by AYColumbia

Why does Get Column Information return "The selected stored procedure or function returns no coumns." when my sprocs do return columns?

# re: Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Wednesday, July 18, 2012 9:31 AM by Jalpesh P. Vadgama

@Ayolumbia can you paste code for stored procedure here.