嵌套的抽象基类和table-per-concrete-class,流畅的nhibernate

时间:2018-04-12 08:31:25

标签: nhibernate fluent-nhibernate fluent-nhibernate-mapping

我在使用流利的nhibernate替换旧版ORM时遇到了问题。我试图弄清楚如何使用Automap将嵌套的抽象类与每个具体类的表映射。与我发现的示例相比,我的层次结构中有两个抽象类。

public abstract class AbstractBaseEntity
{
    public virtual string Id { get; set; } = Guid.NewGuid().ToString();

    public virtual AbstractIntermediateEntity Intermediate { get; set; } 
    public virtual ConcreteEntity Concrete { get; set; } 
}

public abstract class AbstractIntermediateEntity : AbstractBaseEntity
{
    public virtual string Property { get; set; }
}

public class ConcreteEntity : AbstractIntermediateEntity
{
    public virtual string OtherProperty { get; set; }
}

我正在配置我的SessionFactory:

public class Configuration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Type type) => base.ShouldMap(type) && typeof(AbstractBaseEntity).IsAssignableFrom(type);
}

...

var sessionFactory = Fluently.Configure()
    .Database(SQLiteConfiguration.Standard.UsingFile(dataStorage.DataStorage.PathToDbFile))
    .Mappings(m => m.AutoMappings
        .Add(AutoMap.AssemblyOf<AbstractBaseEntity>(new Configuration())
            .Override<AbstractBaseEntity>(_ => _.UseUnionSubclassForInheritanceMapping())
            .IncludeBase<AbstractBaseEntity>()
            .IncludeBase<AbstractIntermediateEntity>()))
    .BuildSessionFactory();

我得到的错误告诉我:

XmlSchemaValidationException: The element 'union-subclass' in namespace 
'urn:nhibernate-mapping-2.2' has invalid child element 'joined-subclass' 
in namespace 'urn:nhibernate-mapping-2.2'.

我理解这一点,你不能混合使用FNH生成的union-subclass和joined-subclass:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class xmlns="urn:nhibernate-mapping-2.2" name="ApplicationFramework.Models.AbstractBaseEntity, ApplicationFramework, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" table="`AbstractBaseEntity`" abstract="true">
    <id name="Id" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="assigned" />
    </id>
    <many-to-one class="ApplicationFramework.Models.AbstractIntermediateEntity, ApplicationFramework, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" name="Intermediate">
      <column name="Intermediate_id" />
    </many-to-one>
    <many-to-one class="ApplicationFramework.Models.ConcreteEntity, ApplicationFramework, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" name="Concrete">
      <column name="Concrete_id" />
    </many-to-one>
    <union-subclass name="ApplicationFramework.Models.AbstractIntermediateEntity, ApplicationFramework, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" abstract="true">
      <property name="Property" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Property" />
      </property>
      <joined-subclass name="ApplicationFramework.Models.ConcreteEntity, ApplicationFramework, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" abstract="true">
        <key>
          <column name="AbstractIntermediateEntity_id" />
        </key>
        <property name="OtherProperty" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="OtherProperty" />
        </property>
      </joined-subclass>
    </union-subclass>
  </class>
</hibernate-mapping>

所以,我的问题是如何使用Automap正确配置FNH?

请注意,我可以使用ClassMap<AbstractBaseEntity>SubclassMap<AbstractIntermediateEntity>(调用Abstract)和SubclassMap<ConcreteEntity>来使用流畅的映射,但我真的应该使用Automap!

谢谢!

0 个答案:

没有答案