在 Nhibernate 中使用 IList,未初始化
我基本上有:
Public Class Job: MyBaseClass
{
public virtual string JobInformation {get;set;}
...
public virtual List<Item> JobItems {get;set;}
}
我正在使用一个列表并在构造函数中初始化它(JobItems = new List();)。但是,我遇到了异常(Nhibernate.Collection.Generic.PersistentGenericBag)
我读了 这个问题 它说我应该使用 IList 代替。
所以现在我有
public class Job: MyBaseClass
{
public virtual string JobInformation {get;set;}
...
public virtual IList<Item> JobItems {get;set;}
}
public virtual void AddItem(Item item)
{
//snip validation
this.JobItems.Add(item);
}
但是,它抛出 NullReferenceException 因为 JobItems 尚未初始化。 NHibernate 什么时候初始化这个集合?或者我该如何解决这个问题?
I basically have:
Public Class Job: MyBaseClass
{
public virtual string JobInformation {get;set;}
...
public virtual List<Item> JobItems {get;set;}
}
I was using a List and initializing it (JobItems = new List();) in the constructor. However, I was getting an Exception (Nhibernate.Collection.Generic.PersistentGenericBag)
I read this question and it said I should use IList instead.
So now I have
public class Job: MyBaseClass
{
public virtual string JobInformation {get;set;}
...
public virtual IList<Item> JobItems {get;set;}
}
public virtual void AddItem(Item item)
{
//snip validation
this.JobItems.Add(item);
}
However, it throws a NullReferenceException because JobItems isn't initialized yet. When does NHibernate initialize this collection? Or how can I solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用支持字段并在返回时确保它不为空
use a backing field and on return make sure it isn't null
如果之前尚未初始化该集合,则应该对其进行初始化。
You should initialize the collection if it hasn't been previously initialized.