我使用EFCode首先在asp.net mvc 3模型中。 (实体框架4.0和EFCode首先0.8) 该模型被定义象下面这样:EFCode首先属性空问题
public class User
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int WorkedYears { get; set; }
}
当使用db.Users.Find(1)
,将引发此错误:
The 'WorkedYears' property on 'User' could not be set to a 'null' value. You must set this property to a non-null value of type 'Int32'.
注:user.Id = 1存在于数据库,并且该记录的WorkedYears为NULL。 如果我在数据库中设置了WorkedYears = 0,错误将消失, 以及如果我定义属性如:public int? WorkedYears{get; set;}
,错误也将消失。 但我不想使用int ?,并且还希望列不保留NULL,如果没有设置。 那么有没有其他解决方案来解决它?
非常感谢。
什么是EFCode First 0.8? EF Code-First的最新版本可以在这里找到:http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-release-candidate-available.aspx它被称为EF 4.1发布候选人。强烈建议升级。 – Slauma 2011-03-31 16:31:14