我使用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