c# - 使用 Entity Framework 读取时出错

标签 c# sql-server entity-framework

我在使用 Entity Framework 从表中读取数据时遇到问题。

我想做的是通过 Id 从表中读取一行。

这是我的 SQL 表定义:

CREATE TABLE [dbo].[Wares] (
    [Ware_ID]       INT           IDENTITY (1, 1) NOT NULL,
    [WareType_ID]   INT           NOT NULL,
    [Model_ID] INT           NOT NULL,
    [Ware_Price]    FLOAT (53)    CONSTRAINT [DF_Wares_Ware_Price] DEFAULT ((0)) NOT NULL,
    [Ware_Count]    INT           CONSTRAINT [DF_Wares_Ware_Count] DEFAULT ((0)) NOT NULL,
    [Ware_Brand]    NVARCHAR (30) CONSTRAINT [DF_Wares_Ware_Brand] DEFAULT (N'') NOT NULL,
    [Ware_Image]    VARCHAR (200) NULL,
    CONSTRAINT [PK_Wares] PRIMARY KEY CLUSTERED ([Ware_ID] ASC),
    CONSTRAINT [FK_Wares_WareTypes] FOREIGN KEY ([WareType_ID]) REFERENCES [dbo].[WareTypes] ([WareType_ID]) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT [FK_Wares_Models] FOREIGN KEY ([PhoneModel_ID]) REFERENCES [dbo].[Models] ([Model_ID]) ON DELETE CASCADE ON UPDATE CASCADE
);

这是我的类定义:

public class Ware
{
    [Key]
    public int Ware_ID { get; set; }

    public int WareType_ID { get; set; }

    public int PhoneModel_ID { get; set; }

    public float Ware_Price { get; set; }

    public int Ware_Count { get; set; }

    public string Ware_Brand { get; set; }

    public string Ware_Image { get; set; }

    public virtual WareType WareType { get; set; }
}

我想从查询字符串中读取 Id 并用它来选择行:

private Ware ThisWare()
{
    string strid = Request.QueryString["id"];
    int id = Convert.ToInt32(strid);

     var context = EFDBContext.ReturnNewContext();
     var query = from m in context.Wares where m.Ware_ID == id select m;

     if (query.Count() == 1)
         return query.First();
     else return null;
}

我从以下行得到错误:return query.First();

这是错误描述:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The 'Ware_Price' property on 'Ware' could not be set to a 'System.Double' value. You must set this property to a non-null value of type 'System.Single'.

有人知道我该如何解决这个问题吗?

P.S.:我已经尝试将 Ware_Price 更改为 System.Single 类型,但它仍然无效。

最佳答案

A float(53)在 SQL 端是 8 个字节。 float (又名 Single )在 C# 端是 4 个字节。 SQL Server 和 .NET 之间的数据类型不匹配。

The 'Ware_Price' property on 'Ware' could not be set to a 'System.Double' value.

这意味着 Entity Framework 收到了一个 System.Double ( double ) 来自表格。

You must set this property to a non-null value of type 'System.Single'.

这意味着您的属性只能接受 System.Single 类型的值(float)。 Entity Framework 识别出差异并响应 InvalidOperationException .

要修复它,您需要修改 Ware_Price Ware 中的数据类型类最高 double .

我进一步建议,对于货币值(value),decimal在 SQL 和 .NET 中都是正确的数据类型,特别是如果您打算用它做任何数学运算(例如,对大量价格求和)。

关于c# - 使用 Entity Framework 读取时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31746122/

相关文章:

c# - ASP .NET Core 2.1-preview2 HttpClient 死锁

sql - 如何在 SQL Server 中有效地查找以特定值结尾的非常大的表中的行?

c# - Entity Framework 迁移 - 未迁移的列

c# - "Include"在 Entity Framework Core 中 SelectMany + Select 后不工作

sql-server - 使用T-SQL,如何捕获操作系统错误代码?

c# - 如何在 Entity Framework 中一次调用使用 UNION(concat/union)?

c# - 升级到 VS 2017 15.6.0,现在 Directory.GetCurrentDirectory() 返回\bin\Debug\netcoreapp2.0

c# - C#获取List中的前几个元素

c# - 通过 WebApi 将文件上传到 Azure Blob 存储,无需访问本地文件系统

c# - 将多行插入 MS SQL Server 并检索所有新表 ID 的背面