0
我的用戶是這樣的:在EF中保存數據時,我是否必須包含虛擬屬性?
public class User
{
public User()
{
Subscribers = new List<SubscriberNumber>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string Username { get; set; }
[Required]
public virtual BusinessNumber BusinessNumber { get; set; }
public string BusinessName { get; set; }
public int SubscriberCount { get; set; }
public List<SubscriberNumber> Subscribers { get; set; }
}
以下調用失敗,因爲一些未公開的EntityValidationError:
var user = db.Users.First(s => s.Username == username);
user.BusinessName = "test";
db.SaveChanges();
但在VS無論是開放user.BusinessNumber
或使用下列內容:
var user = db.Users.Include(s => s.BusinessNumber).First(s => s.Username == username);
得到SaveChanges()
工作。當我更新我的用戶時,是否總是必須包含虛擬屬性?這似乎很麻煩......
您將'BusinessNumber'設置爲'[必需]',因此您必須爲其設置一個值。 – Masoud 2014-10-07 03:57:15