无法使用xml映射的joind-subclass扩展使用NHibernate.Mapping.ByCode构建的映射

问题描述 投票:0回答:1

我正在争议NHibernate.AspNetCore.Identity中描述的NHibernate.Mapping.ByCodehttps://github.com/nhibernate/NHibernate.AspNetCore.Identity/issues/16的映射。

我用NHibernate.AspNetCore.Identity重写了ClassMapping<T>的原始xml映射,代码看起来像这样:

public class IdentityRoleMappingPostgreSql : ClassMapping<IdentityRole> {

    public IdentityRoleMappingPostgreSql() {
        Schema("public");
        Table("aspnet_roles");
        Id(e => e.Id, id => {
            id.Column("id");
            id.Type(NHibernateUtil.String);
            id.Length(32);
            id.Generator(Generators.TriggerIdentity);
        });
        Property(e => e.Name, prop => {
            prop.Column("name");
            prop.Type(NHibernateUtil.String);
            prop.Length(64);
            prop.NotNullable(true);
            prop.Unique(true);
        });
        /* other property mappings ignored here .*/
    }

}

完整的映射代码在这里:

https://github.com/nhibernate/NHibernate.AspNetCore.Identity/tree/master/src/NHibernate.AspNetCore.Identity/Entities

然后我尝试使用xml映射的joined-sublcass扩展这些映射,如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<hibernate-mapping
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns="urn:nhibernate-mapping-2.2"
  namespace="WebTest.Entities" assembly="WebTest">

  <joined-subclass name="AppRole" schema="public" table="app_roles" extends="NHibernate.AspNetCore.Identity.IdentityRole, NHibernate.AspNetCore.Identity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
    <key column="id" />
    <property name="Description" column="description" type="string" length="256" />
  </joined-subclass>

</hibernate-mapping>

设置nhibernate时出现以下异常:

NHibernate.MappingException : These classes referenced by 'extends' were not found:
FullName:NHibernate.AspNetCore.Identity.IdentityRole - Name:NHibernate.AspNetCore.Identity.IdentityRole, NHibernate.AspNetCore.Identity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
FullName:NHibernate.AspNetCore.Identity.IdentityUser - Name:NHibernate.AspNetCore.Identity.IdentityUser, NHibernate.AspNetCore.Identity, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
   at NHibernate.Cfg.MappingsQueue.CheckNoUnavailableEntries()
   at NHibernate.Cfg.Configuration.SecondPassCompile()
   at NHibernate.Cfg.Configuration.BuildMappings()
   at UnitTest.IdentityTest._02_CanSetupMappingByXml()

然后我用JoinedSubclassMapping<AppRole>重写了xml映射,该映射有效,可以建立会话工厂并进行查询而没有任何异常。

public class AppRoleMapping : JoinedSubclassMapping<AppRole> {

    public AppRoleMapping() {
        ExplicitDeclarationsHolder
            .AddAsRootEntity(typeof(NHIdentityRole));
        Extends(typeof(NHIdentityRole));
        Schema("public");
        Table("app_roles");
        Key(k => k.Column("id"));
        Property(
            p => p.Description,
            maping => {
                maping.Column("description");
                maping.Type(NHibernateUtil.String);
                maping.Length(256);
            }
        );
    }

}

所以问题是:

  • [当使用JoinedSubclassMapping<T>扩展使用ClassMapping<T>构建的映射时,它适用于_01_CanExtendByCodeWithByCode
  • [当使用xml映射的joined-subclass扩展使用ClassMapping<T>构建的映射时,获得NHibernate.MappingException : These classes referenced by 'extends' were not found _02_CanExtendByCodeWithXml的异常;
  • [当使用joined-subclass xml映射扩展class xml映射时,它可以工作_03_CanExtendXmlByXml
  • 当使用JoinedSubclassMapping<AppRole>扩展xml的class映射时,它可以工作_04_CanExtendXmlByByCode

完整的测试代码在这里:https://github.com/nhibernate/NHibernate.AspNetCore.Identity/blob/master/test/UnitTest/IdentityTest.cs

也许我做错了什么,或者nhibernate的映射有问题?

c# nhibernate nhibernate-mapping
1个回答
0
投票

在调试并检查NHibernate.Cfg.Configuration的源代码后,有一个内部验证队列来处理xml添加的映射,但是现在可以跳过它,因此简单的解决方法是将HbmMapping转换为xml,然后将xml添加到配置中,让我们仔细检查一下。

var mapper = new ModelMapper();
mapper.AddMapping<IdentityRoleMappingPostgreSql>();
// add other mapping here.
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
// Use AddXml instead of AddMappings
cfg.AddXml(mapping.AsString());
© www.soinside.com 2019 - 2024. All rights reserved.