【问题标题】:NHibernate ClassMap<T> code not executingNHibernate ClassMap<T> 代码未执行
【发布时间】:2012-07-05 15:29:48
【问题描述】:

我正在建立一个新项目,并已让 NHibernate 使用结构映射...排序。我正在使用带有 ClassMaps 的 NHibernate.Mapping.ByCode.Conformist 设置。没有错误发生,但是当我查询会话并且数据库中存在特定类型的记录时,什么也没有返回。经过进一步检查,我为这些类型设置的映射似乎没有执行。这是我的代码,用于连接结构图。我可以确认它正在执行。

public class OrmRegistry : Registry
{
    public OrmRegistry()
    {
        var sessionFactory = BuildSessionFactory();
        For<ISessionFactory>().Singleton().Use(sessionFactory);
        For<ISession>().HybridHttpOrThreadLocalScoped().Use(s => sessionFactory.OpenSession());            

    }

    public ISessionFactory BuildSessionFactory()
    {
        var cfg = new Configuration().DataBaseIntegration(db =>
                                                              {
                                                                  db.ConnectionStringName = "LocalSqlServer";
                                                                  db.Dialect<MsSql2008Dialect>();
                                                                  db.Driver<SqlClientDriver>();
                                                                  db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                                                                  db.IsolationLevel = IsolationLevel.ReadUncommitted;
                                                                  db.BatchSize = 500;
                                                              }).AddAssembly(Assembly.GetExecutingAssembly());

        if(HttpContext.Current != null)
        {
            cfg.CurrentSessionContext<WebSessionContext>();
        }
        else
        {
            cfg.CurrentSessionContext<ThreadStaticSessionContext>();
        }
        return cfg.BuildSessionFactory();
    }
}

我几乎可以肯定我只是在这里遗漏了一些非常明显的东西,但我已经研究了几个小时并且没有取得任何成功。前几天我也变小了,所以我身边没有同事看。

【问题讨论】:

    标签: c# nhibernate nhibernate-mapping structuremap


    【解决方案1】:

    看起来您的配置已初始化,但映射呢?您需要像这样初始化映射(如果您使用约定):

    var mapper = new ConventionModelMapper();
    
    // TODO: define conventions here using mapper instance
    
    // just an example on how I have been using it
    var entities = ... // get all entity types here
    cfg.AddDeserializedMapping(mapper.CompileMappingFor(entities), "MyMappings");
    
    return cfg.BuildSessionFactory();
    

    另一个例子,如果你使用映射类(来自这个post):

    var mapper = new ModelMapper();
    var mappingTypes = typeof (InvoiceMapping).Assembly.GetExportedTypes()
        .Where(t => t.Name.EndsWith("Mapping")).ToArray();
    mapper.AddMappings(mappingTypes);
    
    cfg.AddMapping(mapper.CompileMappingForAllExplicitlyAddedEntities());
    
    return cfg.BuildSessionFactory();
    

    【讨论】:

    • 合二为一。好的。它现在工作得很好。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2012-12-09
    • 2014-06-29
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多