具有故意重复导入的 NHibernate 4.0 代码映射

NHibernate 4.0 code mapping with intentional duplicate import

我怎样才能只使用 NHibernate 4.0 和 CoC 使它工作?

我需要映射两个共享相同名称的不同 类:

namespace MyApp.VersionA {
    public class User{
    //omitted properties
    }
}
namespace MyApp.VersionB {
    public class User{
    //omitted properties
    }
}

这是我的 NHibernate 设置方法:

var config = new Configuration();
config.Configure();
var mapper = new ModelMapper();
mapper.AddMappings(GetAllMappingTypes());
config.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
config.BeforeBindMapping += (sender, args) => args.Mapping.autoimport = false;
Factory = config.BuildSessionFactory();

请注意,我设置了 autoimport=false,但我仍然从 NHibernate 收到 DuplicateMappingException:

nhibernate.duplicatemappingexception: duplicate import:
User refers to both
MyApp.VersionA,
... and
MyApp.VersionB.User,
... (try using auto-import="false")

亚历山大,试试这个:

        var assemblies =
            AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name.Contains(".Infrastructure"));
        foreach (var assembly in assemblies)
        {
            var mapper = new ModelMapper();
            mapper.AddMappings(assembly.GetExportedTypes()
                .Where(t => t.BaseType != null && t.BaseType.IsGenericType &&
                            t.BaseType.GetGenericTypeDefinition() == typeof (ClassMapping<>)));

            var compileMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
            compileMapping.autoimport = false;

            configuration.AddMapping(compileMapping);
        }