Fluent Nhibernate 如何在 SubclassMap 中指定 Id()
我正在使 Fluent NHibernate 适应我们现有的遗留应用程序,并尝试确定如何将 ClassMap 和 SubclassMap 用于所示的实体层次结构。
// BaseObject contains database columns common to every table
public class BaseObject
{
// does NOT contain database id column
public string CommonDbCol1 { get; set; }
public string CommonDbCol2 { get; set; }
// ...
}
public class Entity1 : BaseObject
{
public int Entity1Id { get; set; }
// other Entity1 properties
}
public class Entity2 : BaseObject
{
public int Entity2Id { get; set; }
// other Entity2 properties
}
Entity1 和 Entity2 的标识列按每个表进行唯一命名。 BaseObject 包含所有实体共有的列。我没有使用 AutoMapping,并且认为我可以在 BaseObject 上使用 ClassMap,然后在每个实体上使用 SubclassMap,如下所示:
public class Entity1Map : SubclassMap<Entity1>
{
public Entity1Map()
{
Id(x => x.Entity1Id);
// ...
}
}
问题是,没有为 SubclassMap 定义 Id()。那么,我如何在每个 Entity1Map、Entity2Map...(我们有 100 多个实体类都继承自 BaseObject)中指定实体特定的 Id 是什么?
预先感谢您的任何见解!
I'm in the process of adapting Fluent NHibernate to our existing legacy app and am trying to determine how to use ClassMap and SubclassMap for the entity hierarchy shown.
// BaseObject contains database columns common to every table
public class BaseObject
{
// does NOT contain database id column
public string CommonDbCol1 { get; set; }
public string CommonDbCol2 { get; set; }
// ...
}
public class Entity1 : BaseObject
{
public int Entity1Id { get; set; }
// other Entity1 properties
}
public class Entity2 : BaseObject
{
public int Entity2Id { get; set; }
// other Entity2 properties
}
The identity columns for Entity1 and Entity2 are uniquely named per table. BaseObject contains columns that are common to all of our entities. I am not using AutoMapping, and thought I could use ClassMap on the BaseObject, and then use SubclassMap on each Entity like this:
public class Entity1Map : SubclassMap<Entity1>
{
public Entity1Map()
{
Id(x => x.Entity1Id);
// ...
}
}
Problem is, Id() is not defined for SubclassMap. So, how do I specify within each Entity1Map, Entity2Map, ... (we have 100+ entity classes all inheriting from BaseObject) what the entity-specific Id is?
Thanks in advance for any insight!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Fluent NHibernate 或 NHibernate 中都不可能做到这一点。您实际上希望您的类映射为子类,还是只是希望它们共享公共映射?如果您确实想要子类,那么您将需要让它们共享标识列,没有其他办法;如果您不需要实际的子类,请创建一个抽象的 ClassMap;其中 T : BaseObject 并映射其中的公共属性。
像这样的东西:
It's not possible to do that in either Fluent NHibernate or NHibernate. Do you actualy want your classes to be mapped as subclasses, or do you just want them to share the common mappings? If you truly want subclasses, then you're going to need to have them share the identity column, no other way around it; if you don't want actual subclasses, create an abstract
ClassMap<T> where T : BaseObject
and map the common properties in there.Something like: