带有鉴别器的 Fluent NHibernate AutoMapping
我正在尝试使用鉴别器映射继承,但子类没有鉴别器值。如何使用 AutoMappings 解决这个问题?
域对象如下:
public abstract class Item : GuidIdentityEntity {
public virtual string Name { get; set; }
}
public class Product : Item {}
public class RawMaterial : Item {}
配置如下:
AssemblyOf<Item>()
.IgnoreBase<GuidIdentityEntity>();
.IncludeBase<Item>();
.Setup(setup => {
setup.DiscriminatorColumn = type => "Discriminator";
setup.IsDiscriminated = type => type == typeof(Item);
setup.SubclassStrategy = type => (type == typeof(Item))
? SubclassStrategy.Subclass
: SubclassStrategy.JoinedSubclass;
});
映射结果为:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Solution.Core.Products.Item, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" table="`Item`">
<id name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="guid.comb" />
</id>
<discriminator type="String">
<column name="Discriminator" />
</discriminator>
<property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
<subclass name="Solution.Core.Products.RawMaterial, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" />
<subclass name="Solution.Core.Products.Product, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" />
</class>
</hibernate-mapping>
I'm trying to map inheritance with discriminator, but subclasses don't have discriminator value. How to solve it using AutoMappings?
Domain objects are as following:
public abstract class Item : GuidIdentityEntity {
public virtual string Name { get; set; }
}
public class Product : Item {}
public class RawMaterial : Item {}
Configuration looks like:
AssemblyOf<Item>()
.IgnoreBase<GuidIdentityEntity>();
.IncludeBase<Item>();
.Setup(setup => {
setup.DiscriminatorColumn = type => "Discriminator";
setup.IsDiscriminated = type => type == typeof(Item);
setup.SubclassStrategy = type => (type == typeof(Item))
? SubclassStrategy.Subclass
: SubclassStrategy.JoinedSubclass;
});
Outcome of mappings are:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="Solution.Core.Products.Item, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" table="`Item`">
<id name="Id" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="guid.comb" />
</id>
<discriminator type="String">
<column name="Discriminator" />
</discriminator>
<property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
<subclass name="Solution.Core.Products.RawMaterial, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" />
<subclass name="Solution.Core.Products.Product, Solution.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2e5ef41be3839ad7" />
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现这是可行的:
在这种情况下,我有从 Invoice 类派生的 SalesInvoice 类。当当前子类派生自 Invoice 时,子类的接受标准为 true。鉴别器值是根据子类的类型分配的。
正如您所看到的,我使用名称来比较类型。不幸的是,由于某种原因, subclass.EntityType 属性在运行时为 null,因此我无法直接比较类型。
I found that this works:
In this scenario I have SalesInvoice class deriving from Invoice class. The acceptance criteria for subclass is true when current subclass derives from Invoice. Discriminator value is assigned based on type of subclass.
As you can see, I compare types using names. Unfortunately for some reason subclass.EntityType property is null at runtime so I'm unable to compare types directly.