2012-04-13 89 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。