FluentNHibernate SQL Server配置;连接字符串问题

时间:2011-01-27 09:09:12

标签: c# .net nhibernate fluent-nhibernate

这是我第一次尝试Fluent NH。我将连接字符串存储在Properties.Settings中;

FnhDbString = Data Source=PC\SQLEXPRESS;Initial Catalog=FNHTest;Integrated Security=True

如果我使用.FromAppSetting配置Fluent,我会收到异常:

ArgumentNullException
Value cannot be null. Parameter name: Data Source

如果我使用.FromConnectionStringWithKey配置Fluent,我会收到异常:

NullReferenceException
Object reference not set to an instance of an object.

完整的方法:

private static ISessionFactory CreateSessionFactory()
{ 
      return Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008
        .ConnectionString(c => c
            .FromAppSetting(Properties.Settings.Default.FnhDbString))
        .Cache(c => c
            .UseQueryCache()).ShowSql())
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>())
        .BuildSessionFactory();
}

...

那么我在这里做错了什么..?

1 个答案:

答案 0 :(得分:7)

改变这个:

.ConnectionString(c => c.FromAppSetting(Properties.Settings.Default.FnhDbString))

到此:

.ConnectionString(Properties.Settings.Default.FnhDbString)