Free NHibernate from appSettings

I want to configure my NHibernate Fluent from app.config and appSettingKey.

Is there anyone who can explain what the app.config file should look like?

MsSqlConfiguration.MsSql2005  
   .ConnectionString(c => c  
    .FromAppSetting("appSettingKey")); 

And these are my connections

Data Source=(local);Initial Catalog=ABC;Integrated Security=True

This does not work:

<appSettingKey>"Data Source=.;Initial Catalog=ABC;Integrated Security=True"</appSettingKey>

// Mat, Stockholm, Sweden

+3
source share
3 answers

If you understand correctly, you want to configure Fluent NHibernate as in your example and use the connection string from App.config. Below is an example of how I accomplished this.

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="FluentNHibernateConnection"
      value="server=.;Initial Catalog=YourDB;Integrated Security=True" />
  </appSettings>
</configuration>

Code for creating a factory session:

private static ISessionFactory CreateSessionFactory()
{
    var fluentConfig = MsSqlConfiguration.MsSql2005
        .ConnectionString.FromAppSetting("FluentNHibernateConnection");

    PersistenceModel persistenceModel = new PersistenceModel();
    persistenceModel.addMappingsFromAssembly(typeof(User).Assembly);

    Configuration nhConfig = new Configuration()
        .AddProperties(fluentConfig.ToProperties());

    persistenceModel.Configure(nhConfig);

    return nhConfig.BuildSessionFactory();
}

Hope this helps.

/ Eric ("Stockholm")

+9
source
Fluently.Configure()
                .Database(
                    MsSqlConfiguration.MsSql2008.ConnectionString(
                                c => c.FromConnectionStringWithKey(connectStringKey)
                            )//End ConnectionString
                        )//End Database
                .Mappings(m =>m.FluentMappings.AddFromAssemblyOf<ADomainClassType>())
                .BuildSessionFactory();

This is how I create a factory session.

0
source

Source: https://habr.com/ru/post/1703628/


All Articles