ef 6 and parameterized stored procedures

I am new to entity framework and need some help with stored procs.  can anyone show me or point me to a tutorial that shows how to use ef 6 to create a model that uses parameterized stored procs and get the results into a view?

I have a stored proc that takes 3 input parameters and returns a result set built from 2 different tables.  this is what I want to call in a view.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:

First of all you would need to create a model that would fit the results of your stored procedure.

For instance let's say that the stored procedure would return an id and a name. You would have to create a model (a class):

public class MySPModel
    public int id {get;set;}
    public string name {get;set;}

Open in new window

Now you may use a method in your datacontext.DataBase that enables you to run custom queries. Custom queries van be whatever you would write on an SSMS query window. A simple sql query, or exec a stored procedure.

For this do the following in your controller:

public ActionResult Index(int id)
    var idParam = new SqlParameter
        ParameterName = "id",
        Value = id
    var results = datacontext.DataBase.SqlQuery<MySPModel>("dbo.myStoredProcedureName @id", idParam).ToList();

    return View(results);

Open in new window

Now add a view:

@model List<MySPModel>

@foreach(var model in Model)

Open in new window

As you can understand my example only uses one param, but likewise you can add the other two. If your stored procedure returns from a specific table (lets call it simply TableName) then you could miss the creation of the model as it is already in your edml. so you could call the SqlQuery as:

var results = datacontext.DataBase.SqlQuery<TableName>("dbo.myStoredProcedureName @id", idParam).ToList();

Open in new window

Hope it helps,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bob LearnedCommented:
There is a way to generate the code from the context designer:

Calling Stored Procedures from Entity Framework

The easiest way to work with stored procedures that perform an update is to use the EF Designer...
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.