“在配置中找不到方言”Fluent Nhibernate

时间:2011-06-19 14:30:50

标签: fluent-nhibernate

我在ASP.net MVC 3.0项目中使用Fluent Nhibernate和MSSql 2008。

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

但是,我收到此错误“无法在配置中找到方言”

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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"))