Fluent NHibernate Map 继承类
目前正在开发遗留应用程序,我们正在用 NHibernate 替换数据访问,因为旧的数据访问没有进行急切/延迟加载,系统的某些部分正在执行 2000 多个查询来创建关系......
一切都很好但我完全不知道如何映射一种场景。
另一方面,我们有一个“班次”和一个“多资源班次”
public class Shift
{
public int Id { get; protected set; }
public string EmployeeName { get; set; }
public IEnumerable Breaks { get; set; }
...
}
,多资源班次实际上是一个班次,可以分配多个员工。
public MultiResourceShift : Shift
{
public IEnumerable Employees { get; set; }
public bool IsMultiResource { get; set; }
}
数据库结构被映射为一对多,如下所示:
+--------------------+ | Shift | +--------------------+ | ShiftId | | EmployeeName | | IsMultiResource | | ... | +--------------------+ +--------------------+ | MultiResourceShift | +--------------------+ | MultiResourceId | | ShiftId | | EmployeeId | | ... | +--------------------+
理想情况下,需要查询结果返回 Shift 或 MultiResourceShift 的集合。
目前,这是通过迭代读取器来实现的,如果它是轮班,则将其映射并添加到集合中,如果它是多资源,则创建 MultiResourceShift 的实例并从轮班数据填充,然后加载员工,然后将其添加到集合中。
任何人都知道这是否可能,我将如何映射它并查询它。
Working on a legacy application at the moment and we are replacing the data access with NHibernate, since the old data access didn't do eager/lazy loading, parts of the system were doing 2000+ queries to create relationships...
Everything is fine except I have absolutely no idea how to map one scenario.
We have a 'Shift' and a 'MultiResourceShift'
public class Shift
{
public int Id { get; protected set; }
public string EmployeeName { get; set; }
public IEnumerable Breaks { get; set; }
...
}
Multi Resource Shift on the otherhand, is effectively a Shift, that can have multiple employees assigned to it.
public MultiResourceShift : Shift
{
public IEnumerable Employees { get; set; }
public bool IsMultiResource { get; set; }
}
The database structure is mapped as a one-to-many like:
+--------------------+ | Shift | +--------------------+ | ShiftId | | EmployeeName | | IsMultiResource | | ... | +--------------------+ +--------------------+ | MultiResourceShift | +--------------------+ | MultiResourceId | | ShiftId | | EmployeeId | | ... | +--------------------+
Ideally this needs to be queried where the result returns a collection of Shifts, or MultiResourceShifts.
At the moment this is achieved by iterating over the reader, and if it's a Shift it's Mapped and added to the collection, if it's a Multi Resource, an instance of MultiResourceShift is created and populdated from the shift data, and the Employees are loaded, and it's then added to the collection.
Anyone know if this is possible, how I would map it and query it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://nhforge .org/blogs/nhibernate/archive/2011/02/16/get-load-polymorphism-in-nhibernate-3.aspx
NH博客上的这篇博文正是我所追求的并解决了问题。我们稍微改变了我们的模型来创建鉴别器。
http://nhforge.org/blogs/nhibernate/archive/2011/02/16/get-load-polymorphism-in-nhibernate-3.aspx
This blog post on the NH blog is exactly what I was after and solved the problem. We changed our model slightly to create the discriminator.