0

The goal

I want to call Stored Procedures on my controller with C# + MVC 4 + Entity Framework 5.

The problem

I found this topic that talk about "How to use DbContext.Database.SqlQuery(sql, params) with stored procedure? EF Code First CTP5" and that is exactly what I'm need.

In the topic we have the following code:

context.Database.SqlQuery<myEntityType>(
    "mySpName @param1, @param2, @param3",
    new SqlParameter("param1", param1),
    new SqlParameter("param2", param2),
    new SqlParameter("param3", param3)
);

As you can see, there is a generic type (?) called myEntityType and I do not know who is my entity type.

I'm using Entity Framework 5 and all of my CRUD was generated by him.

Details

What I want it is simple (I guess): display the return of procedure in my view. The above's code is what I have on ProductsController:

//
// GET: /Products/
public ActionResult Index()
{
    return View(db.bm_products.ToList());
}

I want to display the return in this method.

What I have

Maybe myEntityType is bm_products?

Thanks in advance.

Community
  • 1
  • 1
Guilherme Oderdenge
  • 4,935
  • 6
  • 61
  • 96

2 Answers2

1

The myEntityType, or TElement, is the type of object that should be created to house the result. If your procedure did this:

SELECT FieldA FROM table

then you'd need a class like this:

public class myEntityType
{
    public string FieldA { get; set; }
}

where myEntityType is the class name for the type -that's up to you.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
0

What about myEntityType when you are calling a stored procedure which has two output parameters called @OFirstName and @OLastName both of type varchar in a stored procedure. Those two output parameters are not columns of any table.

Ziggler
  • 3,361
  • 3
  • 43
  • 61