C# SaveOrUpdate()上的集合不能为null
当我试图在数据库中保存新用户时,我遇到了一个问题。我的代码:C# SaveOrUpdate()上的集合不能为null,c#,.net,nhibernate,exception-handling,C#,.net,Nhibernate,Exception Handling,当我试图在数据库中保存新用户时,我遇到了一个问题。我的代码: if (!user.ValidateUser(username, password)) { using (var session = NHibernateHelper.OpenSession()) { User u = new User(); u.Name = username; u.Email = string.Empty; u.Password = "
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();
}
}
}
错误:
集合不能为空。参数名称:c
如何在数据库中保存新用户
p.S.session.Save(user1)代码>也给我这个例外
更新:用户类
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; }
}
公共部分类用户
{
公共用户()
{
this.CurrentTestings=new HashSet();
this.resultset=new HashSet();
}
公共虚拟整数Id{get;set;}
公共虚拟字符串名称{get;set;}
公共虚拟字符串密码{get;set;}
公共虚拟字符串电子邮件{get;set;}
公共虚拟ICollection CurrentTestings{get;set;}
公共虚拟ICollection结果集{get;set;}
}
可能类“User”有一些引用,例如:
class User
{
IList<T> T;
}
类用户
{
IList T;
}
尝试使用constractor初始化此字段:
class User
{
public User
{
T = new List<T>();
}
IList<T> T;
}
类用户
{
公共用户
{
T=新列表();
}
IList T;
}
可能类“User”有一些引用,例如:
class User
{
IList<T> T;
}
类用户
{
IList T;
}
尝试使用constractor初始化此字段:
class User
{
public User
{
T = new List<T>();
}
IList<T> T;
}
类用户
{
公共用户
{
T=新列表();
}
IList T;
}
在我的例子中,我需要将集合initzialize从Hashset更改为Hashedset 在我的例子中,我需要将集合initzialize从Hashset更改为Hashedset 请发布用户
类。您如何创建新用户而不是重复使用?请发布用户
类。您如何创建新用户而不是重复使用?