实体框架不能处理一个简单的表变量? [英] Entity Framework can't handle a simple table variable?

查看:147
本文介绍了实体框架不能处理一个简单的表变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 在存储过程中的最后一行: SELECT * FROM @t

  • 更新模型,并且找到存储过程

  • 试图导入使用向导创建新的功能和它说没有列可寻。

真的吗?有人告诉我,这谎言。

  CREATE PROCEDURE WorkIt

设置NOCOUNT ON创建表#pivot

    名称VARCHAR(30)
    值小数,
    等级VARCHAR(2)
)插入#pivot
选择重复厘',1,'K'
UNION ALL
选择重复厘,2,1
UNION ALL
选择重复厘,3,2
UNION ALL
选择重复厘',4'3'
UNION ALL
选择重复厘',5,'4'
UNION ALL
选择重复厘',6,'5'
UNION ALL
选择重复厘',7,'6'
UNION ALL
选择重复厘',8,7
UNION ALL
选择重复厘',9,'8'
UNION ALL
选择重复厘',10,'9'
UNION ALL
选择重复厘,11,10
UNION ALL
选择重复厘,12,11
UNION ALL
选择重复厘,13,12
声明@t表

    名VARCHAR(30)
    ķ十进制(15,5),
    [1]的十进制(15,5),
    [10]十进制(15,5),
    [11]十进制(15,5),
    [12]十进制(15,5),
    [2]的十进制(15,5),
    [3]的十进制(15,5),
    [4]的十进制(15,5),
    [5]的十进制(15,5),
    [6]的十进制(15,5),
    [7]的十进制(15,5),
    [8]的十进制(15,5),
    [9]的十进制(15,5)

插入@t
EXEC dbo.CrossTabWith​​outSumWithOrderBy #pivot,名称,空,甲级,值,
     - 排序重复%,至底部
    案名时,''重复PCT'',那么其他999 0结束删除表#pivot
选择@t *

结果

 名称-K 1 10 11 12 2 3 4 5 6 7 8 9
重复PCT 2.00000 11.00000 12.00000 13.00000 3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 1.00000 10.00000


解决方案

在实体框架试图检索调用存储过程中的列 SET FMTONLY ON 并执行后存储过程。当 FMTONLY ON 执行只返回元数据,并不会在存储过程中的一些先进的施工工作 - 例如动态SQL,临时表也表变量。

您有三种选择:


  • 作为<一个描述href=\"http://stackoverflow.com/questions/4473998/entity-framework-4-the-selected-stored-procedure-returns-no-columns\">another答案添加 SET FMTONLY OFF 在存储过程的开始。这将导致您的存储过程,真正执行所所以一定要确保它只读取数据 - 任何插入,更新或删除您每次尝试检索列时间
  • 将被执行!
  • 手动定义复杂类型

  • 修改您的存储过程不使用任何的这个功能

  • The last line in the stored procedure: select * from @t
  • Updated model and it found the stored procedure
  • Tried to import a new function using the wizard and it said no columns could be found.

Seriously? Someone tell me that it lies.

create procedure WorkIt
as
set nocount on

create table #pivot
(
    Name varchar(30),
    Value decimal,
    Grade varchar(2)
)

insert into #pivot
select 'Repeating Pct', 1, 'K'
union all
select 'Repeating Pct', 2, '1'
union all
select 'Repeating Pct', 3, '2'
union all
select 'Repeating Pct', 4, '3'
union all
select 'Repeating Pct', 5, '4'
union all
select 'Repeating Pct', 6, '5'
union all
select 'Repeating Pct', 7, '6'  
union all
select 'Repeating Pct', 8, '7'
union all
select 'Repeating Pct', 9, '8'
union all
select 'Repeating Pct', 10, '9'
union all
select 'Repeating Pct', 11, '10'
union all
select 'Repeating Pct', 12, '11'
union all
select 'Repeating Pct', 13, '12'
declare @t table
(
    name varchar(30),
    K decimal (15,5) ,
    [1] decimal (15,5),
    [10] decimal (15,5),
    [11] decimal (15,5),
    [12] decimal (15,5),
    [2] decimal (15,5),
    [3] decimal (15,5),
    [4] decimal (15,5),
    [5] decimal (15,5),
    [6] decimal (15,5),
    [7] decimal (15,5),
    [8] decimal (15,5),
    [9] decimal (15,5)
)
insert into @t
exec dbo.CrossTabWithoutSumWithOrderBy #pivot, 'Name', null, 'Grade', 'Value', 
    -- sort repeating pct to bottom
    'case name when ''Repeating Pct'' then 999 else 0 end'

drop table #pivot
select * from @t

Result

name    K   1   10  11  12  2   3   4   5   6   7   8   9
Repeating Pct   2.00000 11.00000    12.00000    13.00000    3.00000 4.00000 5.00000 6.00000 7.00000 8.00000 9.00000 10.00000    1.00000

解决方案

When entity framework tries to retrieve columns from stored procedure it calls SET FMTONLY ON and after that executes the stored procedure. When FMTONLY is ON execution returns only metadata and it doesn't work with some advanced construction in stored procedures - for example dynamic SQL, temporary tables and also table variables.

You have three choices:

  • As described in another answer add SET FMTONLY OFF at beginning of your stored procedure. This will cause your stored procedure to really execute so make sure it only reads data - any insert, update or delete will be executed each time you try to retrieve columns!
  • Manually define complex type
  • Modify your stored procedure to not use any of this features

这篇关于实体框架不能处理一个简单的表变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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