1

I want to execute a stored procedure in Entity Framework and for that, I am using this code:

public dynamic ExecSp(SqlParameter[] Para, string SpName)
{
    try
    {
        var _data = Context.SqlQuery<dynamic>("exec "+ SpName + " "+ string.Join(",", Para.Select(m=>m.ParameterName)), Para);
        return _data 
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

In my code the stored procedure name and parameters are dynamic. When I execute the stored procedure an {object} is returned. I want to map the {object} value to property and value so I can easily use the result.

swatsonpicken
  • 873
  • 1
  • 7
  • 21
Jay
  • 703
  • 9
  • 21
  • Have you want to bind the results for certain model/viewmodel class? You can change `dynamic` type parameter in `Context.SqlQuery` to a designated class name instead converting `object`. – Tetsuya Yamamoto Aug 01 '17 at 06:38
  • In my case store procedure and parameters name are come from database table and i can not fix the return type for that. how i can create class for that and for that i must use `dynamic` type – Jay Aug 01 '17 at 06:42
  • Can you include the stored procedure code? If it's using `SELECT` & returns set of entity class property members defined in EF you can use an entity class name instead of `dynamic`. And what kind of relation with MVC in this issue? – Tetsuya Yamamoto Aug 01 '17 at 06:46
  • No i can't include stored procedure in code. – Jay Aug 01 '17 at 06:48
  • This will be helpful to you @Jay https://stackoverflow.com/q/26749429/1660178 it is similar question – sangram parmar Aug 01 '17 at 06:50
  • EF will ignore properties not returned by the stored procedure when mapping to a class type. In other words, there doesn't have to be a direct 1-1 correlation between the class properties and the SP return. Therefore, you can simply create a class that has a superset of all the properties that could be potentially returned from the SP and use that. – Chris Pratt Aug 01 '17 at 13:15
  • 1
    i will find my solution using @sangramparmar Link. – Jay Aug 01 '17 at 14:19

0 Answers0