2011-06-19 50 views
0

我在ASP.net MVC 3.0項目中使用Fluent Nhibernate和MSSql 2008。「在配置中找不到方言」Fluent Nhibernate


private static ISessionFactory CreateSessionFactory() 
     { 
FluentConfiguration configuration = Fluently.Configure() 
    .Database(MsSqlConfiguration.MsSql2008 
     .ConnectionString("Data Source=Comp;Initial Catalog=MyDB;Integrated Security=true") 
     .ShowSql()).ExposeConfiguration(c => c.SetProperties(AddProperty())) 
    .Mappings(m => m 
     .FluentMappings.AddFromAssemblyOf()); 
     return configuration.BuildSessionFactory(); 
} 

private static IDictionary AddProperty() 
     { 
      IDictionary config = new Dictionary(); 
      config.Add("current_session_context_class", "web"); 
      config.Add(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"); 
      return config; 
     } 

但是,我得到這個錯誤「找不到在配置方言」

回答

1

你可以嘗試這樣的事情

FluentConfiguration configuration = Fluently.Configure() 
          .Database(MsSqlConfiguration.MsSql2008 
          .ConnectionString("Data Source=Comp;Initial Catalog=MyDB;Integrated Security=true") 
          .ShowSql().Dialect("NHibernate.Dialect.MsSql2008Dialect")).ExposeConfiguration(c => c.SetProperties(AddProperty())) 
          .Mappings(m => m.FluentMappings.AddFromAssemblyOf()); 

另外對於WebSessionContext,您可以使用此代碼

FluentConfiguration configuration = Fluently.Configure() 
          .Database(MsSqlConfiguration.MsSql2008 
          .ConnectionString("Data Source=Comp;Initial Catalog=MyDB;Integrated Security=true").CurrentSessionContext<WebSessionContext>() 
          .ShowSql().Dialect("NHibernate.Dialect.MsSql2008Dialect")) 
+1

MsSqlConfiguration.MsSql2008 - 這個初始化方言 – ruskin

+0

是的,這個工作簡單,是很簡單的。您甚至可以將連接字符串更改爲:.ConnectionString(c => c.FromConnectionStringWithKey(「DefaultConnection」))並將其從web.config文件中提取出來。 – Astaar

2

你實際上覆蓋了所有已設置的屬性通過以前的所有配置步驟。嘗試添加到集合來代替:

private static ISessionFactory CreateSessionFactory() 
{ 
    FluentConfiguration configuration = Fluently.Configure() 
     .Database(MsSqlConfiguration.MsSql2008 
      .ConnectionString("Data Source=Comp;Initial Catalog=MyDB;Integrated Security=true") 
      .ShowSql()) 
     .ExposeConfiguration(AddProperties) 
     .Mappings(m => m 
      .FluentMappings.AddFromAssemblyOf()); 

    return configuration.BuildSessionFactory(); 
} 

private static IDictionary AddProperties(NHibernate.Cfg.Configuration config) 
{ 
    config.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, "web"); 
    config.SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"); 
    return config; 
}