你好我一直在研究N-hibernate映射,我遇到了错误:failed to lazily initialize a collection of role in Nhibernate collection
我使用的域名是:
public class User
{
public int Id {get;set;}
public string Name {get;set;}
public ICollection<Reports> Reports {get;set;}
}
public class Reports
{
public int Id {get;set;}
public string Name {get;set;}
public User User {get;set;}
public Reports()
{}
public Reports(User user)
{
this.User = user;
this.User.Reports.Add(this); // this is where error comes
}
}
我正在使用的映射是这样的:
在User.cs中
Bag(x => x.SavedReports, (m) =>
{
m.Inverse(true);
m.Cascade(Cascade.All.Include(Cascade.DeleteOrphans));
m.OrderBy(h => h.CreationDate);
m.Key(k =>
{
k.Column("UserId");
k.ForeignKey("FK_Reports_Users");
});
},
m => m.OneToMany()
);
在Reports.cs中
ManyToOne(x => x.User, m =>
{
m.Column("UserId");
m.NotNullable(false);
});
任何人都可以告诉我我收到此错误的原因。我发现它被抛出,因为用户对象加载了一个惰性集合,并且内部属性没有被提取。