实体框架:执行存储过程以返回不同的列和列数据类型 [英] Entity Framework: Executing a stored procedure that returns different columns and column data types

查看:67
本文介绍了实体框架:执行存储过程以返回不同的列和列数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以这种方式执行存储过程:

I am trying to execute a stored procedure this way:

var filterValues= context.Database.SqlQuery<FilterProcedureDTO>( 
 "[dbo].[sp_GetFilterValues] @FieldID", new SqlParameter("FieldID", filterID))
 .ToList();

问题在于,随着用户更改视图上的过滤器,每次调用时出现的过滤器值都有不同的列名,尽管所有出现的对象都是带有int列和string列的对象,但它们似乎绑定到了指定的模型,即FilterProcedureDTO ..,它类似于

the issue is the filter values that come up have diffrent column name with each call as the user changes the filter on the view,though all come up as objects with an int column and string column,it seems they are bound to the specified model ie FilterProcedureDTO..which looks like

  public class FilterProcedureDTO
    {
        public FilterProcedureDTO() {
        //empty constructor
        }
        public int production_lineID { get; set; }
        public string production_line_desc { get; set; }
    }

因此,如果呼叫产生 taskID task_desc ,则此操作将不再起作用. 另一件事是,有些ID是 int32 ,有些是 int16 ,因此由于周期性的异常,代码无法完美执行

so if the call produces taskID and task_desc this wont work anymore. Another thing is some IDs are int32 and some are int16 so the code is not executing perfectly because of periodic exceptions

如何获取存储过程以返回通用对象,而只是识别数据类型而不是变量名?

How can I get the stored procedure to return generic objects,just recognising the datatypes and not variable names too?

推荐答案

SQLQuery方法始终尝试根据属性名称进行列到属性的匹配.

The SQLQuery method always attempts the column-to-property matching based on property name.

因此,您需要更改存储过程,以使其始终返回相同的名称和数据类型.您应该能够使用别名并在sql中进行强制转换.

So you need to change your stored procedure so that it always returns the same name and datatype. You should be able to do that using aliases and casting in the sql.

参考:

https://stackoverflow.com/a/9987939/150342

这篇关于实体框架:执行存储过程以返回不同的列和列数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆