2

I'm setting up a new project and have gotten NHibernate to work with structuremap...sorta. I'm using the NHibernate.Mapping.ByCode.Conformist setup with ClassMaps. No errors occur, but when I query over a session and there are records present in the database for a particular type, nothing comes back. Upon further examination, it appears that the mappings that I've set up for these types are not executing. Here's my code that wires up things for structuremap. I can confirm that it is being executed.

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

I'm nearly certain I'm just missing something extremely obvious here, but I've been looking at it for a few hours and haven't had any success. I also got downsized a couple days ago, so I don't have a coworker around to look at it.

4

1 に答える 1

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

マッピング クラスを使用している場合の別の例 (この投稿から):

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();
于 2012-07-05T15:54:29.127 に答える