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

本文关键字:映射 NHibernate 代码 导入 | 更新日期: 2023-09-27 18:30:16

如何仅使用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:
用户引用两者
MyApp.VersionA,
...和
MyApp.VersionB.User,
...(尝试使用自动导入="false")

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

亚历山大,试试这个:

        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);
        }