Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

I am Jalpesh Vadgama a Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 6 years of experience in .NET Technology.
Browse by Tags · View All
#ASP.NET 88
#DOTNET 87
BRH 79
ASP.NET 72
#.NET 52
C#.NET 48
DOTNET 47
ASP.NET 4.0 31
ASP.NET MVC 29
VisualStudio 27

Archive · View All
December 2010 16
July 2011 13
April 2011 13
April 2012 12
January 2011 12
June 2011 11
May 2011 11
May 2012 8
February 2013 7
January 2013 7

Entity Framework 4.0- Bind Stored Procedure with Result Entity class

Aug 18 2010 5:18AM by Jalpesh   

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!!!

Shout it

Tags: Linq-To-SQL, Entity, BRH, #DATAACCESS, #DOTNET, #LINQ, LINQ, Entity Framework, EF4.0, Linq-To-Entities, ADO.NET Entity Framework, #ASP.NET,


Jalpesh
15 · 11% · 3548
2
 
0
Lifesaver
 
0
Refreshed
 
 
0
Incorrect



Submit

5  Comments  

  • Great exemplo. Did you try to get only the @@IDENTITY? It;s work out? No matter what I try this never works. To get complex type works fine but when you try to get only one value like @@IDENTITY nothing works.

    commented on Sep 7 2011 2:41PM
    rpicilli
    3015 · 0% · 2
  • Adding more to Jalps post , I would recommend readers to read the to create databases from the model in EF 4.0 .

    This feature is also only available in EF 4.0 but really useful in OR design for creating DB from the model it self.

    commented on Sep 15 2011 5:54AM
    Hima
    31 · 6% · 1776
  • is there any tutorial to insert into database using stored procedure

    commented on Feb 15 2012 5:12AM
    sajan064
    2802 · 0% · 3
  • This was cool. I am just digging into Entity Framework.

    commented on Jul 19 2012 8:38AM
    solutionsville
    2725 · 0% · 4
  • Hi i have store procedure Like

    Create Procedure procedurename
    (
    @Pid int
    )
    as
    begin 
    if(@Pid =1 )
    begin
    Select * From Table 1 
    End
    else if (@Pid =2 )
    begin
    Select * From Table 1 
    End
    End
    

    My Problem is : When i get Column information it will return the Columns from Table one Only. And i cant Get the Result of Table 2 . Please advice.

    commented on Aug 27 2012 12:25AM
    abritman
    3015 · 0% · 2

Your Comment


Sign Up or Login to post a comment.

"Entity Framework 4.0- Bind Stored Procedure with Result Entity class" rated 5 out of 5 by 2 readers
Entity Framework 4.0- Bind Stored Procedure with Result Entity class , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]