2012-04-13 87 views
0

我嘗試在數據庫中保存新用戶時遇到問題。我的代碼:SaveOrUpdate()上收集不能爲空

if (!user.ValidateUser(username, password)) 
{ 
    using (var session = NHibernateHelper.OpenSession()) 
    { 
     User u = new User(); 
     u.Name = username; 
     u.Email = string.Empty; 
     u.Password = "123456"; 
     using (var transact = session.Transaction) 
     { 
      session.SaveOrUpdate(u); // I have an exception in this line 
      transact.Commit(); 
     } 
    } 
} 

錯誤:

Collection cannot be null. Parameter name: c

如何保存新的用戶數據庫?

P.S. session.Save(user1);也給我這個例外。

更新: User類

public partial class User 
{ 
    public User() 
    { 
     this.CurrentTestings = new HashSet<CurrentTesting>(); 
     this.ResultsSet = new HashSet<ResultTesting>(); 
    } 

    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
    public virtual string Password { get; set; } 
    public virtual string Email { get; set; } 

    public virtual ICollection<CurrentTesting> CurrentTestings { get; set; } 
    public virtual ICollection<ResultTesting> ResultsSet { get; set; } 
} 
+0

請問您可以發佈'User'類。 – 2012-04-13 10:01:03

+0

@Rory McCrossan更新 – LuckSound 2012-04-13 10:38:23

+0

你是什麼創建一個新的用戶而不是重複使用? – abatishchev 2012-04-13 10:42:54

回答

3

也許類 「用戶」 具有一定的參考,例如:

class User 
{ 
    public User 
    { 
    T = new List<T>(); 
    } 
    IList<T> T; 
} 
+0

非常感謝你) – LuckSound 2012-04-13 10:41:37

1

class User 
{ 
    IList<T> T; 
} 

嘗試constractor初始化這個領域

在我的情況下,我需要將集合initzialize從Hashset更改爲Ha shedset。