简体   繁体   中英

The specified cast from a materialized 'System.Double' type to the 'System.Int32' type is not valid But no Double defined

Greeting, I am working on a program where I do something I consider very simple, I create a view with three columns defined as string,int and datetime. (view below)

Create View [dbo].[vw_findFaulty]
AS
WITH CTE_getMAXDate AS
(
    Select DeviceId,MAX(Createtime) as maxLastDate
    from locationhistory
    group by DeviceId
)
SELECT B.DeviceName,DeviceId,maxLastDate FROM CTE_getMAXDate A
INNER JOIN dbo.devices B 
ON A.DeviceId=B.id
where maxLastDate < dateadd(week,-3,getdate())

I import the view into my model, when i check the properties of the data type it is , string, int32 and datetime.

However when I call my method to try to load my simple view.

public ActionResult fault()
{ 
     var findFaulty = db.vw_findFaulty;
     return View(findFaulty.ToList());
}

I get the error The specified cast from a materialized 'System.Double' type to the 'System.Int32' type is not valid.

Apart from the fact I cant find any reference to a "double" I am 110% sure that this was working before. Would appreciate if someone could direct me to where I can look to find this error.

Thank you

Suppose database model property looks like this:

public int DeviceId { get; set; }
public string DeviceName { get; set; }
public DateTime maxLastDate { get; set; }

As a quick fix, it is necessary to ensure that the view returns exactly same data type by using CAST clause:

CREATE VIEW [dbo].[vw_findFaulty]
AS
WITH CTE_getMAXDate AS
(
    SELECT DeviceId, MAX(Createtime) AS maxLastDate
    FROM locationhistory
    GROUP BY DeviceId
)

-- explicit cast for DeviceId
-- if DATETIME column also returning similar error, use CAST(maxLastDate AS DATETIME)
SELECT B.DeviceName, CAST(DeviceId AS INT), maxLastDate FROM CTE_getMAXDate AS A
INNER JOIN dbo.devices AS B 
ON A.DeviceId = B.id
WHERE maxLastDate < DATEADD(week, -3, GETDATE())

The problem seems originated from data types passed from view to assigned model class, since binding result set from SQL database requires translation and matching between SQL data types to CLR types (note that there's no guarantee that query results will return expected data type), ie data type for DeviceId returned by query can be mismatched with assigned model type.

Similar issue:

Linq - The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid

I had a similar problem with EntityFramework. Simply selecting two columns out of a table, and creating an anonymous type, eg

ctx.mytable.Select(f=>new{f.column1, f.column2});

Worked in test, not in prod.

The issue was the data type in the production database for column2 was not the same as my entity model (edmx).

On test column2 was a tinyint, on production it was an int. The model was a tinyint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM