【发布时间】:2013-09-03 08:07:58
【问题描述】:
我有一个对象“位置”,它有一个子位置列表。 我的对象和映射看起来像这样
private int _id;
private string _name;
private IList<Location> _subLocations;
private IList<Stock> _stockList;
private Location _parent;
private bool _isActive;
private bool _recommend;
public virtual IList<Location> SubLocations
{
get
{
if (_subLocations == null)
{
_subLocations = new List<Location>();
}
return _subLocations;
}
set
{
_subLocations = value;
OnPropertyChanged("SubLocations");
}
}
// more properties ...
我的映射如下所示:
public class LocationMap:ClassMap<Location>
{
public LocationMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.IsActive);
References(x => x.Parent);
HasMany(x => x.SubLocations).Where(x => x.IsActive == true);
HasMany(x => x.StockList).Where(x => x.IsActive == true);
Table("tbl_locations");
}
}
我也 100% 确定位置对象具有 IsActive = true
但是当我得到一个 Location 对象时,SubLocations 列表总是空的。有人知道这是如何表现的吗?或者有什么想法可以调试这样的问题?
编辑 我的数据库如下所示:
Id Name IsActive Parent_id
1 Magazijn A 1 NULL
2 Magazijn B 1 NULL
3 Gang A 1 2
4 Rek B 1 3
查询 这是我获取所有父位置的查询
public IList<Location> GetAllParentLocations()
{
var result = NHibernateHelper.Session.CreateQuery("from Location l fetch all properties where l.Parent is null and l.IsActive = true").List<Location>();
return (List<Location>)result ?? new List<Location>();
}
生成的 SQL
NHibernate: select location0_.Id as Id15_, location0_.Name as Name15_, location0_.IsActive as IsActive15_, location0_.Parent_id as Parent4_15_ from tbl_locations location0_ where (location0_.Parent_id is null) and location0_.IsActive=1
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 1 [Type: Int32 (0)]
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 42 [Type: Int32 (0)]
NHibernate: SELECT sublocatio0_.Location_id as Location5_1_, sublocatio0_.Id as Id1_, sublocatio0_.Id as Id15_0_, sublocatio0_.Name as Name15_0_, sublocatio0_.IsActive as IsActive15_0_, sublocatio0_.Parent_id as Parent4_15_0_ FROM tbl_locations sublocatio0_ WHERE (sublocatio0_.IsActive = 1) and sublocatio0_.Location_id=@p0;@p0 = 60 [Type: Int32 (0)]
在查询中,它对“Location_id”列执行 where 子句。但它必须使用 Parent_id 列。不确定这个 Location_id 列来自哪里...
【问题讨论】:
-
您查看过正在生成的 SQL 吗?你得到你应该得到的 SQL 了吗?查看 NProf 或启用 log4net
-
我查看了输出,发现查询没有获取子位置,但我不知道为什么。 (我已经编辑了我的帖子)
-
当您遇到断点并观看
GetAllParentLocations().SubLocations时会发生什么?您是否收到异常或延迟加载触发? -
什么都没有发生。但是我再次查看了我的查询,发现我的表也有一个“Location_Id”列。它在 Location_id 而不是 Parent_id 上执行“位置”。但是在我的映射中,我使用的是 Parent...我不知道这个“Location_id”列来自哪里
-
我添加了我的解决方案:HasMany(x => x.SubLocations).KeyColumns.Add("Parent_Id").Where(x => x.IsActive == true);似乎可以解决它。
标签: c# nhibernate mapping