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

这是我第一次尝试Fluent NH.我将连接字符串存储在Properties.Settings中;FnhDbString = Data Source=PC\SQLEXPRESS;Initial Catalog=FNHTest;Integrated Security=True如果我使用.FromAppSetting配置Fluent,我会得到...

这是我第一次尝试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();
}

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

解决方法:

改变这个:

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

对此:

.ConnectionString(Properties.Settings.Default.FnhDbString)

本文标题为:c# – FluentNHibernate SQL Server配置;连接字符串问题

基础教程推荐