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.
double:public double Ware_Price { get; set; }floatandSingleare the same data type. Changing it toSinglewould only produce the original error again.