我在使用 Fluent NHibernate (1.2.0.712) 将每个层次结构的表映射到现有数据库时遇到问题。
这是简化的情况。
我的类结构:
数据库结构:
所以基本上,我们有 1 个表(实体),其中包含一些特定数据(Id 和名称)+ 几个外键(到 ReferencedObject1 或 ReferencedObject2)。
抽象类AbstractEntity包含Id和Name。 ConcreteEntity1 和 ConcreteEntity2 是派生类。
我想根据数据库中的引用映射这些派生实例,所以
如果存在对ReferencedObject1的引用->它是 ConcreteEntity1 的一个实例。
如果存在对ReferencedObject2的引用->它是 ConcreteEntity2 的一个实例。
一些代码:
public class AbstractEntity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
public class ConcreteEntity1 : AbstractEntity
{
public virtual ReferencedObject1 ReferencedObject1 { get; set; }
}
public class ConcreteEntity2 : AbstractEntity
{
public virtual ReferencedObject2 ReferencedObject2 { get; set; }
}
public class AbstractEntityMap : ClassMap<AbstractEntity>
{
public AbstractEntityMap()
{
Table("Entities");
Id(e => e.Id);
Map(e => e.Name);
}
}
所以实际上我的问题是我应该如何映射派生类?
我浏览了 Fluent nhibernate wiki 并发现
如果您想执行每个类层次结构一个表的策略,那么您只需在 ClassMap 中指定鉴别器列即可。
但是,据我了解,我没有鉴别器列。我的情况是根据外键引用确定子类。
注意:对于多个鉴别器列,请使用 DiscrimminateSubClassesOnColumn("").Formula([在此处插入自定义 sql])
我找不到公式使用的好示例。对我的情况有帮助吗?我有点困惑,也许我做错了什么?
有人可以帮助我吗?
提前致谢 :)
I have a problem with table per hierarchy mapping to an existing database using Fluent NHibernate (1.2.0.712).
Here is the simplified case.
My class structure:
Database structure:
So basically, we have 1 table (Entities) which contains some specific data (Id and Name) + several foreign keys (to ReferencedObject1 OR ReferencedObject2).
Abstract class AbstractEntity contains Id and Name. ConcreteEntity1 and ConcreteEntity2 are derived classes.
I would like to map these derived instances depending on references in database, so
If there is a reference to ReferencedObject1 -> it is an instance of ConcreteEntity1.
If there is a reference to ReferencedObject2 -> it is an instance of ConcreteEntity2.
Some code:
public class AbstractEntity
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
}
public class ConcreteEntity1 : AbstractEntity
{
public virtual ReferencedObject1 ReferencedObject1 { get; set; }
}
public class ConcreteEntity2 : AbstractEntity
{
public virtual ReferencedObject2 ReferencedObject2 { get; set; }
}
public class AbstractEntityMap : ClassMap<AbstractEntity>
{
public AbstractEntityMap()
{
Table("Entities");
Id(e => e.Id);
Map(e => e.Name);
}
}
So actually my question is how should I map derived classes?
I've looked through fluent nhibernate wiki and found that
If you wanted to do a table-per-class-hierarchy strategy, then you just need to specify the discriminator column in your ClassMap.
but, as far as I understand, I do not have a discriminator column. My case is to determine subclass depending on foreign key references.
Note: For multiple discriminator columns use a DiscriminateSubClassesOnColumn("").Formula([insert custom sql here])
I can't find a good example of formula usage. Can it be helpful in my case? And I'm a little bit confused, maybe I am doing something wrong?
Could anyone help me?
Thanks in advance :)
发布评论
评论(1)