将变量列数从动态 sql 返回到实体框架的解决方法是什么?
所以我有一个存储过程,它有一些动态sql,它返回计算值。但返回的结果集可以是可变列。
所以,现在的问题是我想将此过程添加到实体框架 4 中,但我认为它不会支持它。所以,现在的一种方法是创建临时表或表值变量会有所帮助,但是我们不想走那条路。
你们有人遇到过这个问题吗?对于这种情况有哪些解决方法。我基本上想从存储过程返回可变数量的列,并以某种方式将其映射到实体框架中的复杂类型?
So I have stored procedure which has some dynamic sql and it returns the calculated values. The returned result set though can be of variable columns.
So, now the problem is I want to add this procedure to entity framework 4 and I don't think its going to support it.So, now one way to do it would be creating temporary tables or table valued variables will kind of help but we don't want to go that way.
Have anyone of you come across this problem? What are the workarounds for such a situation. I basically want to return variable number of columns from a stored procedure and somehow map it to a complex type in entity framework ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方法是您必须修改 SP 以始终返回所有可能的列,并对任何不存在的列使用占位符。例如,如果您的 SP 在一种情况下返回 col1、col2 和 col3,在另一种情况下返回 col1、col2、col4 和 col5,那么您需要始终返回 col1、col2、col3、col4 和 col5,并使用虚拟值 (null或空字符串,例如)对于您没有值的列。例如,
关于 EF 需要记住的一点是,当它生成复杂类型时,它基本上会调用 SP 并为所有参数传递 null。无论返回什么结果集,EF 在创建复杂类型时都会使用什么结果集。
我们发现这是 EF 中最令人沮丧的事情之一。
The workaround is that you have to modify your SP to always return all possible columns, and you use placeholders for any that aren't present. For example, if your SP returns col1, col2 and col3 in one scenario, and col1, col2, col4 and col5 in another scenario, then you need to always return col1, col2, col3, col4 and col5, and use dummy values (null or empty string, for example) for the columns that you don't have values for. E.g.
The thing to remember about EF is that when it generates the complex type, it basically calls your SP and passes null for all the parameters. Whatever result set gets returned is what EF is going to use when creating the complex type.
We've found this one of the more frustrating things with EF.