I am using FluentNHibernate and trying to configure Environment.DefaultFlushMode
to FlushMode.Commit
According to this SO question it should be possible: How to change default FlushMode to Commit in C#?
Indeed, this feature was added https://nhibernate.jira.com/browse/NH-3619
Here is how I use it:
public ISessionFactory BootstrapSessionFactory()
{
return Fluently.Configure()
.ExposeConfiguration(cfg =>
cfg.SetProperty(Environment.DefaultFlushMode, FlushMode.Commit.ToString()))
.BuildSessionFactory();
}
According to source code of FluentConfiguration
public FluentConfiguration ExposeConfiguration(Action<Configuration> config)
{
if (config != null)
{
this.configAlterations.Add(config);
}
return this;
}
public Configuration BuildConfiguration()
{
try
{
MappingConfiguration mappingConfiguration = new MappingConfiguration(this.logger);
foreach (Action<MappingConfiguration> mappingsBuilder in this.mappingsBuilders)
{
mappingsBuilder(mappingConfiguration);
}
mappingConfiguration.Apply(this.Configuration);
if (this.cache.IsDirty)
{
this.Configuration.AddProperties(this.cache.Create());
}
foreach (Action<Configuration> configAlteration in this.configAlterations)
{
configAlteration(this.Configuration);
}
I can see that my config alterations are being applied when I debug
However, when I actually check session.FlushMode
for a session built using this sessionFactory
I'm getting some other FlushMode
that I did not set.
What am I missing? What am I doing wrong?
Thanks a lot!
PS: versions of libraries used are:
<PackageReference Include="FluentNHibernate" Version="2.1.2" />
<PackageReference Include="NHibernate" Version="5.1.3" />
Update:
Digging a bit deeper, I can see that the code above indeed sets the SessionFactory.DefaultFlushMode
correctly, however, this flush mode is not being applied to session
This is how i get ISession
and ISessionFactory
public static void Install(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<PersistenceSetup>();
// ISessionFactory
serviceCollection.AddSingleton(provider => provider.GetService<PersistenceSetup>().BootstrapSessionFactory());
// ISession
serviceCollection.AddTransient(provider => provider.GetService<ISessionFactory>().OpenSession());
And, as a result Flush modes are different when I debug: