使用实体框架阅读时出错

时间:2021-07-01 22:38:01

I have a problem with reading data from a table with Entity Framework.

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

What I'm trying to do is read a row from table by Id.

我想要做的是从Id读取一行。

This is my SQL table definition:

这是我的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
);

And this is my class definition:

这是我的班级定义:

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; }
}

I want to read the Id from a query string and use it to select the row:

我想从查询字符串中读取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;
}

I get the error from the line: return query.First();

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

This the error description:

这个错误描述:

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

EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

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'.

附加信息:'Ware'上的'Ware_Price'属性无法设置为'System.Double'值。您必须将此属性设置为“System.Single”类型的非null值。

Does anybody know how I can fix this?

有谁知道我怎么解决这个问题?

P.S.: I have already tried changing Ware_Price to System.Single type but it still didn't work.

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

1 个解决方案

#1


9  

A float(53) on the SQL side is 8 bytes. A float (aka Single) on the C# side is 4 bytes. You have a data type mismatch between SQL Server and .NET.

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

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

'Ware'上的'Ware_Price'属性无法设置为'System.Double'值。

This means Entity Framework received a System.Double (double) from the table.

这意味着实体框架从表中收到System.Double(double)。

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

您必须将此属性设置为“System.Single”类型的非null值。

This means your property can only accept values of type System.Single (float). Entity Framework recognized the difference and reacted with an InvalidOperationException.

这意味着您的属性只能接受System.Single(float)类型的值。实体框架识别出差异,并对InvalidOperationException做出反应。

To fix it, you need to bump the Ware_Price data type in your Ware class up to a double.

要修复它,您需要将Ware类中的Ware_Price数据类型提高到两倍。

I would further suggest that for monetary values, decimal would be the correct data type, both in SQL and .NET, especially if you plan on doing any math with it (summing a large number of prices, for example).

我进一步建议,对于货币值,十进制将是正确的数据类型,在SQL和.NET中,特别是如果你打算用它做任何数学(例如,总结大量的价格)。

#1


9  

A float(53) on the SQL side is 8 bytes. A float (aka Single) on the C# side is 4 bytes. You have a data type mismatch between SQL Server and .NET.

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

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

'Ware'上的'Ware_Price'属性无法设置为'System.Double'值。

This means Entity Framework received a System.Double (double) from the table.

这意味着实体框架从表中收到System.Double(double)。

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

您必须将此属性设置为“System.Single”类型的非null值。

This means your property can only accept values of type System.Single (float). Entity Framework recognized the difference and reacted with an InvalidOperationException.

这意味着您的属性只能接受System.Single(float)类型的值。实体框架识别出差异,并对InvalidOperationException做出反应。

To fix it, you need to bump the Ware_Price data type in your Ware class up to a double.

要修复它,您需要将Ware类中的Ware_Price数据类型提高到两倍。

I would further suggest that for monetary values, decimal would be the correct data type, both in SQL and .NET, especially if you plan on doing any math with it (summing a large number of prices, for example).

我进一步建议,对于货币值,十进制将是正确的数据类型,在SQL和.NET中,特别是如果你打算用它做任何数学(例如,总结大量的价格)。