Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error While reading with entity framework

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

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

This is my SQL table definition:

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:

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();

This the error description:

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

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.

like image 311
A.Moshayyedy Avatar asked Jul 31 '15 12:07

A.Moshayyedy


1 Answers

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.

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

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

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

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

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

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

like image 81
Cᴏʀʏ Avatar answered Oct 19 '22 11:10

Cᴏʀʏ