Fluent NHibernate HasMany 集合未填充
我在设置 Fluent NHibernate HasMany 集合时遇到问题。
我已将代码设置如下,并通过 Linq IQueryable 调用它。 在 SQL Profiler 中,我可以看到调用了正确的 SQL,但 Store.Staff 集合始终为空。
public class Store
{
public virtual IList<Employee> Staff { get; set; }
public virtual void AddEmployee(Employee Employee)
{
Employee.Store = this;
if(Staff == null)
Staff = new List<Employee>();
Staff.Add(Employee);
}
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.StoreId)
.GeneratedBy.Identity();
HasMany(x => x.Staff)
.Inverse()
.Cascade.All();
...
}
}
public bool Create(Store entity)
{
var stores = _readRepository.Query<Store>()
.Where(x => x.StoreId == entity.StoreId)
.Fetch(x => x.Staff)
.ToList();
select store0_.StoreId,
staff2_.SurgeryId,
staff2_.StoreId
from dbo.[Store] store0_
left outer join dbo.[Employee] staff2_
on store0_.StoreId = staff2_.StoreId
where store0_.StoreId = 1 /* @p0 */
感谢您的任何帮助。
I am having trouble setting up a Fluent NHibernate HasMany collection.
I have set the code up as below and I'm calling it via Linq IQueryable.
In SQL Profiler, I can see the correct SQL getting called, but the Store.Staff collection is always empty.
public class Store
{
public virtual IList<Employee> Staff { get; set; }
public virtual void AddEmployee(Employee Employee)
{
Employee.Store = this;
if(Staff == null)
Staff = new List<Employee>();
Staff.Add(Employee);
}
public class StoreMap : ClassMap<Store>
{
public StoreMap()
{
Id(x => x.StoreId)
.GeneratedBy.Identity();
HasMany(x => x.Staff)
.Inverse()
.Cascade.All();
...
}
}
public bool Create(Store entity)
{
var stores = _readRepository.Query<Store>()
.Where(x => x.StoreId == entity.StoreId)
.Fetch(x => x.Staff)
.ToList();
select store0_.StoreId,
staff2_.SurgeryId,
staff2_.StoreId
from dbo.[Store] store0_
left outer join dbo.[Employee] staff2_
on store0_.StoreId = staff2_.StoreId
where store0_.StoreId = 1 /* @p0 */
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定这就是问题所在,但您应该使用 FetchMany 来急切地加载集合,而不是 Fetch。
Not sure this is the problem, but you should be using FetchMany to eagerly load collections, not Fetch.
我混淆了框架的两个部分。我正在使用 Linq 检索数据,但无法立即加载。
我现在使用 NHibernate.Session.QueryOver,而不是使用 Linq。
I was confusing two parts of the framework. I was using Linq to retrieve the data and couldn't eagerly load.
Instead of using Linq, I now use NHibernate.Session.QueryOver.