这是我第一次尝试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();
}
...
那么我在这里做错了什么..?
答案 0 :(得分:7)
改变这个:
.ConnectionString(c => c.FromAppSetting(Properties.Settings.Default.FnhDbString))
到此:
.ConnectionString(Properties.Settings.Default.FnhDbString)