问题描述
我正在使用FluentNHibernate并尝试将Environment.DefaultFlushMode
配置为FlushMode.Commit
I am using FluentNHibernate and trying to configure Environment.DefaultFlushMode
to FlushMode.Commit
根据这个SO问题,应该是可能的:如何将默认FlushMode更改为在C#中提交?
According to this SO question it should be possible:How to change default FlushMode to Commit in C#?
实际上,已添加此功能 https://nhibernate.jira.com/browse/NH -3619
Indeed, this feature was added https://nhibernate.jira.com/browse/NH-3619
这是我的用法:
public ISessionFactory BootstrapSessionFactory()
{
return Fluently.Configure()
.ExposeConfiguration(cfg =>
cfg.SetProperty(Environment.DefaultFlushMode, FlushMode.Commit.ToString()))
.BuildSessionFactory();
}
根据FluentConfiguration的源代码
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
但是,当我实际检查session.FlushMode
是否使用此sessionFactory
建立的会话时,我得到了我未设置的其他FlushMode
.
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.
我想念什么?我在做什么错了?
非常感谢!
PS:使用的库版本为:
PS: versions of libraries used are:
<PackageReference Include="FluentNHibernate" Version="2.1.2" />
<PackageReference Include="NHibernate" Version="5.1.3" />
更新:深入研究,我可以看到上面的代码确实正确设置了SessionFactory.DefaultFlushMode
,但是,此刷新模式是未应用于会话
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
这就是我得到ISession
和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:
推荐答案
我不明白为什么您的代码无法正常工作.只要成功建立了会话工厂并从中创建会话,它就可以正常工作.
I could not understand why your code does not work. As long as your session factory is built successfully and you are creating session from same, it should work.
虽然我正在使用NHibernate 4.1.0.4000;您正在使用较高版本.我不认为这是由于NHibernate版本造成的.我怀疑这是由于Fluent和NHibernate之间的兼容性问题造成的.
I am using NHibernate 4.1.0.4000 though; you are using upper version. I do not think this is due to version of NHibernate. I doubt this is due to compatibility issue between Fluent and NHibernate.
这与您的问题没有直接关系,但是我使用的是NHibernate Configuration by Code而不是Fulent.
This is not directly relevant to your problem but I am using NHibernate Configuration by Code instead of Fulent.
Configuration configuration = new Configuration();
configuration.SessionFactory().DefaultFlushMode(FlushMode.Commit);
sessionFactory = configuration.BuildSessionFactory();
这会将默认冲洗模式设置为我选择的冲洗模式.当我使用如下所示的会话工厂创建会话时-
This sets default flush mode to the one I choose. When I create session using this session factory like below -
ISession session = sessionFactory.OpenSession();
此会话保持正确的刷新模式(在这种情况下为提交").
this session hold correct ("Commit" in this case) flush mode.
更好的解决方案是通过代码切换到NHibernate自己的配置.这将绕过所有兼容性问题.如果您决定切换以下名称空间:
Better solution is to switch to NHibernate's own configuration by code; this will bypass all compatibility issues. Following are the namespaces if you decide to switch:
using NHibernate.Cfg;
using NHibernate.Cfg.MappingSchema;
这篇关于NHibernate流畅地为Session/SessionFactory配置默认刷新模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!