Open beginor opened 4 years ago
After debug and check the source code of NHibernate.Cfg.Configuration, there is an internal validation queue to process the mappings added by xml, but no way to skip it. So the simple work around is converting the HbmMapping
to xml, then add the xml to configuration, let's double check it.
var mapper = new ModelMapper();
mapper.AddMapping<IdentityRoleMappingPostgreSql>();
// add other mapping here.
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
// Use AddXml instead of AddMappings
cfg.AddXml(mapping.AsString());
I do not know if mixing hbm and By-Code mappings is an "accidental" feature or if it is supposed to work flawlessly in any cases. So I do not really know if the trouble your report should be qualified as a bug or as a feature request.
For now I settle on feature request.
I think it should be a feature too. Maybe we should provide an optional parameter for mapping by xml, let users to specify explicited root entity, provide similar function like the ExplicitDeclarationsHolder.AddAsRootEntity()
method in mapping by code.
I've just run into this and I can provide a very good (at least IMO) reason why this scenario should be supported.
That's because of issue #1277, for which the workaround is essentially "Use an .hbm.xml
mapping instead of mapping-by-code for the affected class". This has meant that I have needed to mix-and-match MbC class mappings and a single XML embedded resource mapping (which just happened to be for a <subclass />
) in the same environment.
FWIW, the workaround @beginor described (convert the MbC mappings to XML & import as XML, then add .hbm.xml
mappings) did work for me.
I have noticed that the workaround: using MBC, then convert those mappings to XML and add them that way, is somewhat less viable from NHibernate 5.4.1 onward. That's if those mappings contain any one-to-one relationships, because of a new crash bug adding the mappings. See #3607 for more information.
I am refacting the mapping of NHibernate.AspNetCore.Identity to
NHibernate.Mapping.ByCode
, as descripted in the issue https://github.com/nhibernate/NHibernate.AspNetCore.Identity/issues/16 .I have rewrite the orignal xml mapping of
NHibernate.AspNetCore.Identity
withClassMapping<T>
, the code looks like this:The full mapping code is here:
https://github.com/nhibernate/NHibernate.AspNetCore.Identity/tree/master/src/NHibernate.AspNetCore.Identity/Entities
Then I try to extend these mappings with
joined-sublcass
of xml mapping, like this:I got the following exception when setting up nhibernate:
Then I rewrite the xml mapping with
JoinedSubclassMapping<AppRole>
, which works , can build session factory and query without any exception.So the issue is:
JoinedSubclassMapping<T>
to extend the mappings build withClassMapping<T>
, it works _01_CanExtendByCodeWithByCode;joined-subclass
of xml mapping to extend the mappings build withClassMapping<T>
, get an exception ofNHibernate.MappingException : These classes referenced by 'extends' were not found
_02_CanExtendByCodeWithXml;joined-subclass
xml mapping to extendclass
xml mapping, it works _03_CanExtendXmlByXml;JoinedSubclassMapping<AppRole>
to extendclass
mapping of xml, it works _04_CanExtendXmlByByCode;The full test code is here: https://github.com/nhibernate/NHibernate.AspNetCore.Identity/blob/master/test/UnitTest/IdentityTest.cs
Maybe there something I do wrong, or some issues with nhibernate's mappings?