C# LINQ到SQL错误。-该类型可以';不能转换为可为空的值

C# LINQ到SQL错误。-该类型可以';不能转换为可为空的值,c#,C#,有时,当我使用LINQtoSQL时,我会收到一条消息,错误是:;“无法将类型转换为可为null的值” 有人知道原因吗 谢谢。sql中的列不可为null,您正在传入一个可以为null的值 比如传球 DateTime? createdon 要创建表,需要createdon日期时间。考虑以下示例,表/字段: Customer.CustomerId (int) Customer.Code (int) Sale.CustomerId (nullable<int>) 考虑另一个表/字段

有时,当我使用LINQtoSQL时,我会收到一条消息,错误是:;“无法将类型转换为可为null的值”

有人知道原因吗


谢谢。

sql中的列不可为null,您正在传入一个可以为null的值

比如传球

 DateTime? createdon 

要创建表,需要createdon日期时间。

考虑以下示例,表/字段:

Customer.CustomerId (int)
Customer.Code (int)
Sale.CustomerId (nullable<int>)
考虑另一个表/字段:

Customer.CustomerId (int)
Customer.Code (int)
Sale.CustomerId (nullable<int>)
到(整数?)


再试一次

有时您希望将可空值(如
int?或可空值
等)赋给非空值(如int)。 为避免出现此异常,必须先将对象强制转换为可为null的值,然后再将任何值赋给它。如以下代码所示:

int a = 1; //a is int and not null value
int? b = 2; //b is nullable int
a = b; //with this line code you will see compile error
a = b.value; //this is the correct syntax

您应该发布一个示例,以提供更多上下文。听起来您的类型无法转换为可空值。模糊的问题需要模糊的答案。