【问题标题】:Database is throwing an InvalidOperationException status of 500 in a MVC Web Application while accessing data from an SQL View从 SQL 视图访问数据时,数据库在 MVC Web 应用程序中抛出 InvalidOperationException 状态 500
【发布时间】:2014-07-24 07:01:18
【问题描述】:

我正在开发一个 Asp.Net MVC 应用程序,其中我有一个包含五列的 SQL 视图,并且我有以下模型

public class InferredBid
    {
        public int Id { get; set; }
        public string Market { get; set; }
        public string Term { get; set; }
        public string Bid { get; set; }
        public string Offer { get; set; }
    }

SQL 视图

Create VIEW [dbo].[InferredBids]
AS
with numbered as
(
    select id, product, grade, term, bid, offer, termid, row_number() OVER (Partition BY Product, Grade ORDER BY termid) i
    from  dbo.CanadianCrudes
)

select r1.i as Id, r1.product + '/' + r1.grade as Market, r1.term + '/' + r2.term as Term, r1.Bid - r2.Offer [Bid], r1.Offer - r2.Bid [Offer] 
from numbered r1 join numbered r2 on  r1.product = r2.product and r1.grade = r2.grade and r1.termid+1=r2.termid and r1.i<r2.i and r1.term!=r2.term          


GO

当我打开应该从上述 SQL 视图呈现数据的页面时。它只是给我一个InvalidOperationException

<m:type>System.InvalidOperationException</m:type>
<m:stacktrace/>
<m:internalexception>
<m:message>
The 'Id' property on 'InferredBid' could not be set to a 'Int64' value. You must set this property to a non-null value of type 'Int32'.
</m:message>

我尝试将属性设置为 int32,但仍然遇到同样的问题。

【问题讨论】:

  • 另外,int 只是 System.Int32 的 C# 别名。与 long 相同 - 它是 System.Int64 等的别名。因此 intSystem.Int32 可以互换使用。

标签: c# sql sql-server asp.net-mvc sql-view


【解决方案1】:

将您的代码更改为以下内容,因为 SQL 似乎正在为该字段返回 Int64(相当于 C# 中的 long)。

public class InferredBid
{
    public long Id { get; set; }
    public string Market { get; set; }
    public string Term { get; set; }
    public string Bid { get; set; }
    public string Offer { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多