如何在除AOP 外的代码中获取TempItems 返回

SqlSugar 沟通中
8 539

在OnDiffLogEvent AOP中添加了TempItems,希望在执行完insert操作之后获取到在OnDiffLogEvent  中添加的TempItems 如何获取,目前测试的结果是如果开启了同步事务 可以获取到,但是如果不开启事务,或者是异步事务则获取不到


image.pngimage.png

热忱回答8

  • image.pngimage.pngimage.png

    0 回复
  • 这是我的sqlsugar注入代码【

    public static void AddSqlSugarSetup(this IServiceCollection services)

    {

        if (services.IsNullOrEmpty()) throw new ArgumentNullException(nameof(services));

        SnowFlakeSingle.WorkId = 1;//配置sqlsugar雪花WorkId 注意服务器 本地的不要设置一样的


        StaticConfig.CompleteInsertableFunc =

        StaticConfig.CompleteUpdateableFunc =

        StaticConfig.CompleteDeleteableFunc = diffLogModel =>

        {

            //反射的方法可能多个就需要用GetMethods().Where

            var method = diffLogModel.GetType().GetMethod("EnableDiffLogEvent");

            method?.Invoke(diffLogModel, [null]);

        };


        var dbConfigList = AppSettingHelper.GetConfigure<GlobalSugarConfig>("DBS").Where(w => w.Enabled).ToList();


        dbConfigList.ForEach(config =>

        {

            var connectionConfig = new ConnectionConfig()

            {

                DbType = config.DBType,

                IsAutoCloseConnection = true,

                ConfigId = config.ConnId.ToLower(),

                LanguageType = LanguageType.Chinese,

                InitKeyType = InitKeyType.Attribute,

                ConnectionString = config.Connection,

                ConfigureExternalServices = new ConfigureExternalServices

                {

                    EntityService = (property, column) =>

                    {

                        if (property.Name == "Id" && !column.IsPrimarykey) column.IsPrimarykey = true;

                    }

                },

                MoreSettings = new ConnMoreSettings()

                {

                    IsAutoRemoveDataCache = true,

                    IsNoReadXmlDescription = true,//禁止读取XML中备注,提升性能

                }

            };

            BaseDBConfig.ValidConfig.Add(connectionConfig);

        });


        services.AddSingleton<ISqlSugarClient>(client =>

        {

            var db = new SqlSugarScope(BaseDBConfig.ValidConfig, db =>

            {

                BaseDBConfig.ValidConfig.ForEach(config =>

                {

                    var dbProvider = db.GetConnectionScope(config.ConfigId.ToString());

                    dbProvider.Aop.OnLogExecuting = (s, parameters) =>

                    {

                        SqlSugarAop.OnLogExecuting(RegexTableName(s),

                                  Enum.GetName(typeof(SugarActionType), dbProvider.SugarActionType), s, parameters,

                                  config);

                    };

                    dbProvider.Aop.OnError = (exp) =>

                    {

                        LogHelper.Error("执行SQL错误事件", exp);

                    };

                    dbProvider.Aop.OnDiffLogEvent = (diff) =>

                    {

                        var aaa = 11;

                        db.TempItems.Add("zheshi","你好");

                    };

                });

            });

            return db;

        });

    }

    】这是我的工作单元

    public class UnitOfWorkManage(ISqlSugarClient sqlSugarClient) : IUnitOfWorkManage

    {


        public SqlSugarScope GetDbClient() => sqlSugarClient as SqlSugarScope;


        #region 同步事务

        public void BeginTran() => GetDbClient().BeginTran();


        public void CommitTran()

        {

            GetDbClient().CommitTran();

        }


        public void RollbackTran() => GetDbClient().RollbackTran();

        #endregion



        #region 异步事务

        public async Task BeginTranAsync() => await GetDbClient().BeginTranAsync();


        public async Task CommitTranAsync()

        {

            await GetDbClient().CommitTranAsync();

        }


        public async Task RollbackTranAsync() => await GetDbClient().RollbackTranAsync();

        #endregion

    }

    工作单元注册方式为作用域

    0 回复
  • fate sta fate sta VIP0
    2025/7/26

    db.ado.isanytran

    0 回复
  • image.png这个是做什么用的?


    0 回复

  • @fate sta

    0 回复
  • @fate sta:不用我自己封装的就可以拿得到 ,是我封装的有问题?可以帮我看一下上面的UnitofWork吗image.png


    0 回复
  • fate sta fate sta VIP0
    2025/7/26
    0 回复
  • fate sta fate sta VIP0
    2025/7/26

    删掉OBJ和BIN打包上传

    0 回复