在NHibernate中使用loquacious类映射

时间:2011-07-04 09:59:11

标签: c# .net nhibernate nhibernate-mapping

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

此致

根据反应找到答案

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

2 个答案:

答案 0 :(得分:1)

您可以尝试以下几点:

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()获取映射类型列表。

答案 1 :(得分:1)

如果您已经在使用Fluent,最简单的方法是保留单独的映射文件并在启动时加载它们。所以我有一个初始化类,它定义了一个类级变量

    private ModelMapper _mapper = new ModelMapper();

然后我调用Initialize()

    public void Initialize()
    {
        Configure = new Configuration();
        Configure.SessionFactoryName(System.Configuration.ConfigurationManager.AppSettings["SessionFactoryName"]);
        Configure.DataBaseIntegration(db =>
                                      {
                                        db.Dialect<MsSql2008Dialect>();
                                        db.Driver<SqlClientDriver>();
                                        db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                                        db.IsolationLevel = IsolationLevel.ReadCommitted;
                                        db.ConnectionStringName = ConnectionStringName;
                                        db.BatchSize = 20;
                                        db.Timeout = 10;
                                        db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
                                      });
        Configure.SessionFactory().GenerateStatistics();

        Map();
    }

然后我的Map()方法就是这个

    private void Map()
    {
        _mapper.AddMappings(MappingAssembly.GetExportedTypes());
        Configure.AddDeserializedMapping(_mapper.CompileMappingForAllExplicitlyAddedEntities(), "MyWholeDomain"); 
    }

我喜欢通过appSettings传递我的映射程序集名称。

相关问题