9

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.

6
  • 1
    When you change the type to System.Single, does it change the error message ? Commented Jul 31, 2015 at 12:25
  • Try chaging it to a double: public double Ware_Price { get; set; } Commented Jul 31, 2015 at 12:27
  • @cosmo0 no ti doesn't it gives the same error. Commented Jul 31, 2015 at 12:36
  • @sstan when I change it to double it will give me an error saying that my model has changed and I should use migration to change the database too. Commented Jul 31, 2015 at 12:37
  • @cosmo0: float and Single are the same data type. Changing it to Single would only produce the original error again. Commented Jul 31, 2015 at 12:40

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

3 Comments

in the database design windows it just wrote the word "float" without any number without any other parameters and it wrote number 53 only in tsql. nevertheless moving to decimal type worked like a charm. Thanks a lot.
Just keep in mind that there are actually two ranges of float(n) values in SQL server: float(1-24) will be 4 bytes and will map to a .NET float, and float(25-53) that will need to map to a double. The default for n is 53, which explains the behavior you saw. Regardless, IMO you should stick with decimal(n, 2) and decimal for values representing currency.
@Cᴏʀʏ, i have same issue in my service. I changed to double, double?,Single, Single?, Double, Double? and I still receive this error message. I use VS2017(Portuguese). Today, i'll go to install VS2015 instead. I don't get solve this problem. 1 week and nothing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.