c# - 不能使用 xml 映射的连接子类来扩展使用 NHibernate.Mapping.ByCode 构建的映射
问题描述
我正在重构 NHibernate.AspNetCore.Identity到的映射NHibernate.Mapping.ByCode
,如问题https://github.com/nhibernate/NHibernate.AspNetCore.Identity/issues/16中所述。
我已经重写了NHibernate.AspNetCore.Identity
with的原始 xml 映射ClassMapping<T>
,代码如下所示:
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 .*/
}
}
完整的映射代码在这里:
然后我尝试使用joined-sublcass
xml 映射扩展这些映射,如下所示:
<?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()
然后我用 重写了 xml 映射JoinedSubclassMapping<AppRole>
,它可以正常工作,可以建立会话工厂和查询,没有任何异常。
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; - 当使用
joined-subclass
xml 映射来扩展构建的映射时ClassMapping<T>
,会出现NHibernate.MappingException : These classes referenced by 'extends' were not found
_02_CanExtendByCodeWithXml的异常; - 当使用
joined-subclass
xml 映射扩展class
xml 映射时,它可以工作_03_CanExtendXmlByXml; - 当
JoinedSubclassMapping<AppRole>
用于扩展class
xml 的映射时,它可以工作_04_CanExtendXmlByByCode;
也许我做错了什么,或者 nhibernate 的映射有一些问题?
解决方案
调试并检查NHibernate.Cfg.Configuration的源代码后,有一个内部验证队列来处理 xml 添加的映射,但现在可以跳过它,所以简单的解决方法是将 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());
推荐阅读
- javascript - 为什么从这个href获取值时看不到值?
- javascript - 向事件监听器添加多个元素
- php - 直接从 Android Studio 进行测试时,应用程序运行良好,但在设备上作为 APK 安装时则不行
- javascript - 如何在角度材料 datepicker 中更改 Datepicker Adapter Runtime
- xamarin - 如何在 Xamarin 中 AutoHeight CollectionView 项目
- kubernetes - Kubernetes 上的工作负载设置以实现高可用性
- python - 调用 CRS.from_epsg 导致 SQLite 错误 (rasterio)
- c# - 在 ASP.NET Core 中一次生成 100-500 个任务(每个 HTTP 请求)是一种不好的做法吗?
- react-native - React Native Invariant Violation:“backdropFilter”不是有效的样式属性
- java - 覆盖静态方法会产生错误