简体   繁体   English

转换为值类型“Int32”失败

[英]The cast to value type 'Int32' failed

I have the following code. 我有以下代码。 I'm getting error: 我收到错误:

The cast to value type 'System.Int32' failed because the materialized value is null. 转换为值类型'System.Int32'失败,因为实现的值为null。 Either the result type's generic parameter or the query must use a nullable type. 结果类型的泛型参数或查询必须使用可空类型。

var productItem =   from sales 
                      in db.Sales_inv_details
                  select new { 
                           sales.Sales_Invoices.Customer.Cust_Id, 
                           sales.Sales_Invoices.Customer.Customer_Name,         
                           sales.Sales_Invoices.Employe.Emp_Name, 
                           sales.Sales_Invoices.Employe.Emp_code,
                           sales.Sales_Invoices.Date_invoice, 
                           sales.Item.Item_ID, 
                           sales.Qty, 
                           sales.Qty_Price, 
                           sales.Item.Item_Name, 
                           sales.Item.Item_Code, 
                           Unit_Measure = sales.Item.TBL_Unit_Measure.Code,
                           sales.Sales_Invoices.Descrption, 
                           sales.disc };

productItem = productItem
  .Where(x => (x.Date_invoice >= model.Fromdate & x.Date_invoice <= (model.Todate)));

productItem = productItem
  .Where(x => (x.Emp_code == model.Emp_code));

It sounds like your model defines one of the properties as non-nullable, but the database has the corresponding column as nullable, and has a null value in it. 听起来您的模型将其中一个属性定义为非可空,但数据库将相应的列定为可为空,并且其中包含空值。 When the generated reader attempts to populate the model, BOOM. 当生成的阅读器尝试填充模型时,BOOM。 Unfortunately, we can't tell you which property, and it doesn't need to even be one of those mentioned in that code, since it could be the FrobNumber property of whatever Sales_inv_details represents. 不幸的是,我们无法告诉您哪个属性,它甚至不需要是该代码中提到的属性之一,因为它可能是Sales_inv_details代表的FrobNumber属性。

You need to check your model very carefully. 您需要非常仔细地检查您的模型。 Start with the properties / columns on whatever Sales_inv_details represents. Sales_inv_details表示的属性/列开始。 When you have found a mismatch: mark it as nullable, and retry. 找到不匹配项后:将其标记为可为空,然后重试。

One of the properties on one of the Entity classes is defined as int ( System.Int32 ), but in the database is a null value. 其中一个Entity类的属性之一定义为intSystem.Int32 ),但在数据库中是null值。 Since int cannot be null this error comes up. 由于int不能为null因此会出现此错误。

To fix this either make sure the database query returns no null values, or change your property definition to a nullable integer ( int? or Nullable<int> ). 要解决此问题,请确保数据库查询不返回空值,或将属性定义更改为可以为null的整数( int?Nullable<int> )。

To find the respective records in your Database try re-creating the query in SQL Management Studio (ie setting the same "WHERE filters"), and check the results for empty columns that should contain integer values. 要在数据库中查找相应的记录,请尝试在SQL Management Studio中重新创建查询(即设置相同的“WHERE过滤器”),并检查应包含整数值的空列的结果。

Looks like wrong mapping db fields to the model. 看起来错误的将db字段映射到模型。 Probably you have int property in your model, but int? 可能你的模型中有int属性,但int? in database 在数据库中

It's hard to tell without having the entity model, but this exception usually happens when the query involves some left outer join , so although LINQ to Entities does not generate NRE if you don't include null checks for the right (optional) side, it requires you to promote non nullable value types into nullable inside the projections. 没有实体模型很难分辨,但是当查询涉及一些left outer join ,通常会发生此异常,因此如果您没有为右(可选)端包含null检查,LINQ to Entities不会生成NRE,要求您在投影内将非可空值类型提升为可空。

Check some of the following relations: sales.Sales_Invoices , sales.Sales_Invoices.Customer , sales.Sales_Invoices.Employe , sales.Item , sales.Item.TBL_Unit_Measure . 检查下面的一些关系: sales.Sales_Invoicessales.Sales_Invoices.Customersales.Sales_Invoices.Employesales.Itemsales.Item.TBL_Unit_Measure If some of the right end properties are optional, make sure to include cast to the corresponding nullable type in the projection, ie if you have int property, use = (int?).. in the select clause (or C#6 ?. operator). 如果某些右端属性是可选的,请确保在投影中包含强制转换为相应的可空类型,即如果您有int属性,请在select子句中使用= (int?).. (或C#6 ?.运营商)。

PS The exception message indicates Int32 , so concentrate on int columns. PS异常消息指示Int32 ,因此专注于int列。

Try to handle the null values generated /existing null values by use of 尝试通过使用来处理生成的空值/现有的空值

  • (int?) (INT?)
  • (p.property == null) ? (p.property == null)? 0 : p.property 0:p.property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 转换为值类型&#39;Int32&#39;失败空值LINQ - The cast to value type 'Int32' failed null value LINQ 转换为值类型“Int32”失败,因为实现值为null - The cast to value type 'Int32' failed because the materialized value is null 转换为值类型“Int32”失败,因为物化值为 null - The cast to value type 'Int32' failed because the materialized value is null Linq错误:强制转换为值类型&#39;Int32&#39;,因为物化值为null - Linq error: The cast to value type 'Int32' failed because the materialized value is null 转换为值类型“Int32”失败,因为通过加载网格视图实现的值为空 - The cast to value type 'Int32' failed because the materialized value is null via loading Grid View 转换为值类型&#39;int32&#39;失败,因为实例化值为null - Cast to value type 'int32' failed because the materialized value is null 将uint转换为Int32 - Cast uint to Int32 我可以使用隐式转换器将常见值类型(例如Int32)的类型转换扩展到我的自定义类型吗? - Can i extend type cast of common value types (like Int32) with implicid converter to my custom type? 无法将参数值从Int32转换为DateTime - Failed to convert parameter value from a Int32 to a DateTime 无法将参数值从字符串转换为Int32错误 - Failed to convert parameter value from a String to a Int32 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM