4

It's my first attempt on Fluent NH. I store the connection string in Properties.Settings;

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

If I configure Fluent with .FromAppSetting I get exception:

ArgumentNullException
Value cannot be null. Parameter name: Data Source

If I configure Fluent with .FromConnectionStringWithKey I get exception:

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

The full method:

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

...

So what am I doing wrong here..?

1 Answer 1

7

Change this:

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

to this:

.ConnectionString(Properties.Settings.Default.FnhDbString)
Sign up to request clarification or add additional context in comments.

2 Comments

I'm curious why this worked? I have a similar problem using the lamba syntax that apparently doesnt work? Trying to use the ConnectionStringFromKey method
That worked because FromAppSetting needs a appSetting key value which you can access using ConfigurationManager.AppSettings["key"];. Secondly, he was directly passing the value of the key FnhDbString from resources, i.e. the connection string, to the FromAppSetting method, which obviously fails. Just passing the connection string directly to the parent method words without issues.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.