本文介绍了如何将默认的FlushMode更改为在C#中提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以...已经说过了:
$ b $ p如何将 FlushMode 更改为在C#中提交



我的意思是,在Fluent NHibernate FlushMode默认设置为Auto。



所以...设置FluentMode为提交,我需要打开会话,然后更改它:

  var someSessionFactory = ... bla bla ..; 
var session = someSessionFactory.OpenSession();
session.FlushMode = FlushMode.Commit;

这将工作但是...这意味着我需要调用包含 FlushMode.Commit 每次我是开幕式。
为了让sessionFactory自动化,我有几个包装(意思是只设置一次,然后在打开新的上下文时自动使用它),这意味着我不能每次都直接打开会话而不需要挖掘工厂类型等等。



有没有办法将 FlushMode 从Auto更改为Commit?有没有办法做到这一点 var sessionFactory = Fluently.Configure()。 ...



编辑

尝试过的第二件事情

pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ this._sessionFactory = ()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(this._connectionString)
.AdoNetBatchSize(10)
.QuerySubstitutions(true 1,假0,是'是',否'N'))
.Cache(c => c.Not.UseSecondLevelCache()。Not.UseQueryCache())
.Mappings(m => ;
{
foreach(mappingAssemblies中的程序集asm)
{
m.FluentMappings.AddFromAssembly(asm);
m.HbmMappings.AddFromAssembly(asm);

))
.ExposeConfiguration(ModifyConfiguration)
.BuildSessionFactory();

var session = _sessionFactory.OpenSession();

$ b $ public void ModifyConfiguration(NHibernate.Cfg.Configuration configuration)
{
configuration.Properties [default_flush_mode] = FlushMode.Commit.ToString();

我调用了 _sessionFActory.OpenSession()来查看FlushMode是否已经改变,并且没有。仍然FlushMode是自动,而不是提交。

解决方案

没有办法如何配置默认FlushMode为ISession。我这样做的方式以及常见的方式是进入 ISessionFactory.OpenSession() (IoC, MVC AOP过滤器,Web API委托),并分配FlushMode (手动)

  var session = SessionFactory.OpenSession(); 
session.FlushMode = FlushMode.Commit;

检查以下内容:





  • 我们可以这样做:

     流利的.Configure()
    。 Database(ConfigureDatabase())
    .Mappings(ConfigureMapping)
    .ExposeConfiguration(ModifyConfiguration)
    .BuildConfiguration();


    $ b private void ModifyConfiguration(Configuration configuration)
    {
    configuration.Properties [default_flush_mode] =Commit;
    }


    So... already said it:

    How to change FlushMode to Commit in C#?

    I mean, In Fluent NHibernate FlushMode by default is setted as Auto.

    So... to set FluentMode to Commit, I need to open session and then change It:

    var someSessionFactory = ... bla bla ..;
    var session = someSessionFactory.OpenSession();
    session.FlushMode = FlushMode.Commit;
    

    This will work but... this will mean that I need to call method which contains FlushMode.Commit each time I am opening session.To inicialize sessionFactory I have several wraps (meant to set it only once and then auto use it when new context is opened), which means I can't just open session directly every time I want without digging into factory type and etc.

    Is there a way to change default FlushMode from Auto to Commit? Is there a way to do it in var sessionFactory = Fluently.Configure(). ... ?

    EDIT:

    Tried seccond thing

    public void Initialise(params Assembly[] mappingAssemblies)
    {
        this._sessionFactory = Fluently.Configure()
            .Database(
                MsSqlConfiguration.MsSql2008
                    .ConnectionString(this._connectionString)
                    .AdoNetBatchSize(10)
                    .QuerySubstitutions("true 1, false 0, yes 'Y', no 'N'"))
            .Cache(c => c.Not.UseSecondLevelCache().Not.UseQueryCache())
            .Mappings(m =>
            {
                foreach (Assembly asm in mappingAssemblies)
                {
                    m.FluentMappings.AddFromAssembly(asm);
                    m.HbmMappings.AddFromAssembly(asm);
                }
            })
            .ExposeConfiguration(ModifyConfiguration)
            .BuildSessionFactory();
    
        var session = _sessionFactory.OpenSession();
    }
    
    public void ModifyConfiguration(NHibernate.Cfg.Configuration configuration)
    {
        configuration.Properties["default_flush_mode"] = FlushMode.Commit.ToString();
    }
    

    I called _sessionFActory.OpenSession() to see if FlushMode has changed and... Nope. Still FlushMode is Auto, instead of Commit.

    解决方案

    There is no way how to configure default FlushMode for ISession. The way I do that, and the way which could be found in common, is step into ISessionFactory.OpenSession() (IoC, MVC AOP Filter, Web API delegate) and assign the FlushMode (manually)

    var session = SessionFactory.OpenSession();
    session.FlushMode = FlushMode.Commit;
    

    Check these:

    The property ISession.FlushMode as defined below:

    public interface ISession : IDisposable
    {
        ...
        /// <summary>
        /// Determines at which points Hibernate automatically flushes the session.
        /// 
        /// </summary>
        /// 
        /// <remarks>
        /// For a readonly session, it is reasonable to set the flush mode 
        ///  to <c>FlushMode.Never</c>
        ///  at the start of the session (in order to achieve some 
        ///       extra performance).
        /// 
        /// </remarks>
        FlushMode FlushMode { get; set; }
    

    and it's the default implementation snippet:

    public sealed class SessionImpl : ...
    {
        ...
        private FlushMode flushMode = FlushMode.Auto;
        ...
    

    is not set anyhow during the ISessionFactory.OpenSession() call.

    ORIGINAL, not working approach

    The documented <hibernate-configuration> setting default_flush_mode is not supported.

    and based e.g. on this Q & A:

    NHibernate config properties in Fluent NHibernate

    we can do:

    Fluently.Configure()
        .Database(ConfigureDatabase())
        .Mappings(ConfigureMapping)
        .ExposeConfiguration(ModifyConfiguration)
        .BuildConfiguration();
    
    ...
    
    private void ModifyConfiguration(Configuration configuration)
    {
        configuration.Properties["default_flush_mode"] = "Commit";
    }
    

    这篇关于如何将默认的FlushMode更改为在C#中提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 16:21