NHibernate ClassMap <t>代码未执行</t>

时间:2012-07-05 15:29:49

标签: c# nhibernate nhibernate-mapping structuremap

我正在建立一个新项目并让NHibernate使用structuremap ... sorta。我正在使用带有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();
    }
}

我几乎可以肯定我只是错过了一些非常明显的东西,但我已经看了几个小时并且没有取得任何成功。我几天前也缩小了规模,所以我没有一个同事来看它。

1 个答案:

答案 0 :(得分:2)

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

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