c# - Entity Framework 不能处理一个简单的表变量?

标签 c# entity-framework-4

  • 存储过程中的最后一行:select * from @t
  • 更新模型并找到存储过程
  • 尝试使用向导导入新函数,但它说找不到任何列。

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

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

结果

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

最佳答案

当 Entity Framework 尝试从存储过程中检索列时,它会调用 SET FMTONLY ON,然后执行存储过程。当 FMTONLYON 时,执行仅返回元数据,并且它不适用于存储过程中的某些高级构造 - 例如动态 SQL、临时表和表变量。

你有三个选择:

  • another answer 中所述在存储过程的开头添加 SET FMTONLY OFF。这将导致您的存储过程真正执行,因此请确保它只读取数据 - 每次您尝试检索列时都会执行任何插入、更新或删除操作!
  • 手动定义复杂类型
  • 修改您的存储过程以不使用任何此功能

关于c# - Entity Framework 不能处理一个简单的表变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5997033/

相关文章:

c# - 在导航属性上定义数据注释

c# - 具有单元格边框的 OpenXML SDK

entity-framework-4 - Entity Framework 代码优先 : How can I determine the foreign key property used for a navigation property at runtime?

entity-framework - Entity Framework CTP 5 一对一映射

c# - TCP 连接的可靠性如何?

c# - 并发访问数据库——防止两个用户获取相同的值

asp.net-mvc-4 - Asp.Net Json-Call 中带有 using 子句的 Dbcontext

c# - 如果 .Count == 0 条件运算符不显示

c# - Partcover 和 Typemock 集成

c# - 使用正则表达式解析 tnsnames.ora