EF4 SPROC 复杂类型映射 - ROW_NUMBER() 问题

发布于 2024-10-07 22:35:59 字数 910 浏览 5 评论 0原文

我使用 EF4/存储过程/复杂类型已经有一段时间了,但我以前没有见过这个问题。

我有一个存储过程,它返回一堆字段,映射到复杂类型的集合。一切都工作正常,直到我引入这个额外的字段。

它使用 T-SQL 中的ROW_NUMBER(用于排名结果):

SELECT ... 
       ROW_NUMBER() OVER (ORDER BY [Field1], [Field2]) AS [SearchRank],
       ...
FROM   @ResultSet

在我的复杂类型中,我将此设置为不可为空的 Int32,并且我也在使用POCO 的,所以我将其作为 POCO 上的常规 int

但是当我尝试执行查询时,我收到此错误:

System.InvalidOperationException: “SearchRank”属性 “RankedLocationSearchResult”无法 设置为“Int64”值。你必须 将此属性设置为非空值 类型为“Int32”。

我就是不明白。我没有在任何地方说过这个属性/字段是 Int64。我的属性类型为“Int32”的非空值。

现在,我确信问题出在 ROW_NUMBER() 上。

因为如果我将该 T-SQL 更改为 1 AS [SearchRank] (硬代码,用于测试),它就可以正常工作。

这几乎就像 EF 将 ROW_NUMBER() 视为返回 Int64 一样。

为什么?我们是否必须将其转换为 32 位整数或其他什么?

有人遇到过这个问题吗?

I've been working with EF4/Stored Procedures/Complex Types for a while now, but i haven't seen this issue before.

I have a stored proc which returns a bunch of fields, mapped to a collection of complex types. Was all working fine until i introduced this extra field.

It's using ROW_NUMBER from T-SQL (used in ranking results):

SELECT ... 
       ROW_NUMBER() OVER (ORDER BY [Field1], [Field2]) AS [SearchRank],
       ...
FROM   @ResultSet

In my complex type, i have this set as a non-nullable Int32, and i'm also using POCO's, so i have this as a regular int on the POCO.

But when i try and execute the query, i get this error:

System.InvalidOperationException: The
'SearchRank' property on
'RankedLocationSearchResult' could not
be set to a 'Int64' value. You must
set this property to a non-null value
of type 'Int32'.

I just don't get it. Nowhere have i said this property/field is Int64. And my property is a non-null value of type 'Int32'.

Now, i am certain the problem is with ROW_NUMBER().

Because if i change that T-SQL to just 1 AS [SearchRank] (hard code, for testing), it works fine.

It's almost as like EF sees ROW_NUMBER() as returning Int64.

Why? Do we have to cast this as a 32-bit integer or something?

Anyone had this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佼人 2024-10-14 22:35:59

因此,在阅读了 ROW_NUMBER() 的 MSDN 文档之后,结果是该函数的返回类型是bigint

所以我必须将复杂类型更改为Int64,并将我的 POCO 属性更改为long

然后就可以了。

So after reading up on the MSDN documentation for ROW_NUMBER(), turns out the return type for this function is bigint.

So i had to change the complex type to Int64, and my POCO property to long.

Then it works.

紫竹語嫣☆ 2024-10-14 22:35:59

我使用带有 ROW_NUMBER() 的视图来生成唯一 id。

就我而言,即使我将复杂类型更改为 Int64 并将 POCO 属性更改为 long,它也不起作用。

我必须从我的视图中删除 ROW_NUMBER() 用法。我最终合并了视图中使用的表并创建了一个表。就是这样运作的。

I was using a view with ROW_NUMBER() usage for unique id generation.

In my case, even if I changed the complex type to Int64 and the POCO property to long, it did not work.

I had to remove the ROW_NUMBER() usage from my view. I finally merged the tables used in my view and created a single table. It worked that way.

离笑几人歌 2024-10-14 22:35:59

我在查询本身中将 ROW_NUMBER() 返回值转换为 INT,如下所示:

SELECT ... 
       CAST(ROW_NUMBER() OVER (ORDER BY [Field1], [Field2]) AS INT) AS [SearchRank],
   ...
FROM   @ResultSet

I Casted the ROW_NUMBER() return value to INT in the query itself as follows:

SELECT ... 
       CAST(ROW_NUMBER() OVER (ORDER BY [Field1], [Field2]) AS INT) AS [SearchRank],
   ...
FROM   @ResultSet
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文