Fluent NHibernate - 混合每个子类表和每个类层次结构表
给出以下结构,
MyBaseClass {
public int Id {get; private set;}
}
MySubclassWithDiscriminator : MyBaseClass {
}
MySubclass : MyBaseClass {
public string SomeThing {get; set;}
}
我将如何使用 Fluent NH 结合使用每个子类表和每个类层次结构表来正确映射这些结构?我尝试过自定义 AutomappingConfiguration,但似乎在兜圈子:
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.Namespace.Contains("Entities");
}
public override bool IsDiscriminated(Type type)
{
// only classes with additional properties should be
// using the table-per-subclass strategy
if ((type.IsAssignableFrom(typeof(MyBaseClass)) ||
type.IsSubclassOf(typeof(MyBaseClass)) &&
type.GetProperties(BindingFlags.Public |
BindingFlags.FlattenHierarchy)
.Count() <= 1))
{
return true;
}
return false;
}
}
public class SubclassConvention : ISubclassConvention
{
public void Apply(ISubclassInstance instance)
{
// Use the short name of the type, not the full name
instance.DiscriminatorValue(instance.EntityType.Name);
}
}
从我的调查来看,在使用 FNH 时,鉴别器的使用是一种二元选择,而 HBM 能够拥有鉴别器列和同时是一个子类。
Give the following structure,
MyBaseClass {
public int Id {get; private set;}
}
MySubclassWithDiscriminator : MyBaseClass {
}
MySubclass : MyBaseClass {
public string SomeThing {get; set;}
}
How would I use Fluent NH to map these correctly, using a combination of table-per-subclass and table-per-class-hierarchy? I've tried a custom AutomappingConfiguration, but seem to be going around in circles:
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Type type)
{
return type.Namespace.Contains("Entities");
}
public override bool IsDiscriminated(Type type)
{
// only classes with additional properties should be
// using the table-per-subclass strategy
if ((type.IsAssignableFrom(typeof(MyBaseClass)) ||
type.IsSubclassOf(typeof(MyBaseClass)) &&
type.GetProperties(BindingFlags.Public |
BindingFlags.FlattenHierarchy)
.Count() <= 1))
{
return true;
}
return false;
}
}
public class SubclassConvention : ISubclassConvention
{
public void Apply(ISubclassInstance instance)
{
// Use the short name of the type, not the full name
instance.DiscriminatorValue(instance.EntityType.Name);
}
}
It seems to me from my investigation that the use of the Discriminator is a binary choice when using FNH, while an HBM has the ability to have a Discriminator column and a subclass at the same time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑 - 2011-05-12
我重写了这篇文章以尝试解决 James Gregory 的评论。
这是我试图实现的 HBM:
因此,我所看到的是生成的 HBM 是
code> 没有
或
子元素,绝不是两者的组合。我在这里错过了什么吗?这是一个失败的测试,可以将其添加到 SubclassPersistenceModelTests 中进行说明:
EDIT - 2011-05-12
I rewrote this post to try to address James Gregory's comments.
Here's the HBM that I'm trying to achieve:
So, what I've seen is that the HBM that's generated is EITHER a
<joined-subclass>
or<subclass>
without the<join>
sub-element, never a combination of the two. Am I missing something here?Here's a failing test that can be added to the SubclassPersistenceModelTests to illustrate: