c# - 在 NHibernate 中使用冗长的类映射

标签 c# .net nhibernate nhibernate-mapping

我正在尝试使用我们用来摆脱 Fluent NHibernate 的新的 loquacious 类映射功能。但是我对如何将类映射到配置感到困惑。我已经阅读了 F Maulo 的博客 ( http://fabiomaulo.blogspot.com/2011/04/me-on-fluent-nhibernate.html ),但我似乎无法让它工作。你们有没有关于如何将其添加到 NH 配置的信息?

问候,

根据 react 找到答案

  foreach (string mappingAssembly in MappingsAssembly.Split(','))
        {
            if (string.IsNullOrEmpty(mappingAssembly)) continue;
            Assembly assembly = Assembly.Load(mappingAssembly);
            types.AddRange(assembly.GetTypes());
            //  configuration.AddAssembly(assembly);
        }
        configuration.DataBaseIntegration(x => GetDatabaseConfig(x));

        ModelMapper mapper = new ModelMapper();

        mapper.AddMappings(types);
        var compiledMapping = mapper.CompileMappingForAllExplicitAddedEntities();

        configuration.AddMapping(compiledMapping);

最佳答案

您可以尝试以下方法:

ModelMapper mapper = new ModelMapper();
// your mappings by code
Type[] types = new Type[] {}; // your mapped types here
// compile mappings
HbmMapping mapping = mapper.CompileMappingFor(types);
// create configuration
Configuration cfg = new Configuration();
// add mappings to config
cfg.AddDeserializedMapping(hbm,"documentname");

请记住,通过代码进行映射会给您带来很多新的可能性:例如:您可以使用 Assembly.GetTypes() 获取映射类型的列表。

关于c# - 在 NHibernate 中使用冗长的类映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6569916/

相关文章:

c# - 管理 Nhibernate 关系表的更好方法

c# - HttpClient.getAsync() 内存不足异常

c# - WCF 泛型类

c# - 如何覆盖 WcF 对象的 ToString 方法?

nhibernate - C# 事件记录实现

caching - 使用 NHibernate.Caches.Redis 时,锁的超时值应该是多少?

c# - 使用 Autofac 进行非注册类型初始化

c# - 下载 zip + 文件到内存中

c# - WPF/银光 : How to convert hex value into Color?

c# - 如何将复选框列表的多个值传递给代码后面的存储过程?