简体   繁体   中英

Execute Stored Procedure in Entity Framework doesn't return data

I have a stored procedure in SQL Server, this procedure returns a resultset with data, or an error message.

I have a view model, with the fields that stored procedure retrieves in the resultset, and I am executing the procedure like the next instruction

IEnumerable<StateVM> states= context.Database.SqlQuery<StateVM>(
                "Calculate_State_1  {0}, {1}, {2}, {3}, {4}, {5}, {6}",
                _from, _to, 8, 'Type1, Id1, Id2, StrError);

When I executed the query with the parameters in SQL, they are data, but in Entity Framework "states" return something related to the specified conversion from type System.String materialized to System.Int32 are not valid.

What I am doing wrong?

You didn't post your error message exactly, but it seems your procedure results in text in for a collumn that is and integer in the StateVM class so EF can't do this conversion.

Check for nulls, empty strings and trailing white spaces in your resultset

您应该能够像处理表一样使用存储过程,并像执行方法调用一样执行它们。

Consider not using EF for your stored procedure calls and instead use ADO.Net. It is very well documented and any issues you run into are bound to be solved many times over.

ADO.Net Using Stored Procedures

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM