How to fetch data from StoreProcedure using Entity Framework ?


Hi

If you will create the Data model in EF using general approach then you will be not able to fetch data from SP in EF.

For fetching data from SP using EF. we can do like this

Step 1 : Add the SP and Table Name in Data Model like this

EF1

Step 2: In the data model designer, right-click on Model Browser then go to storeprocedure

EF2

Step 3: click on “Add Function Import” and configure the SP maping like image

EF3

Step 4: Click on save all and build the application

Step 5: Write the code in code behind file like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (DatabaseModel.DatabaseEntities dc = new DatabaseModel.DatabaseEntities())
        {
            //var query = dc.VW_tblEmp.Select(m => m);
            var query = dc.GetEmpName();
            Grid1.DataSource = query;
            Grid1.DataBind();
        }
    }
}

Note: Here i hope that you are aware of doing configuration on model in EF.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.