我想实现在AOP的更新事件中判断某个特定列,使其不更新,该如何实现呢`(*>﹏<*)′? 返回

SqlSugar 沟通中
3 270

方案一:

尝试使用

entityInfo.EntityColumnInfo.IsIgnore = true

更新倒是不更新了,不过查询变得不查询此列了,失败


方案二:

在AOP中,使用无实体查询,重新给我这个列赋值,间接去实现不更新的效果,

第一次进入AOP,没有问题,更新语句中的值是我重新赋值的值;第二次进入AOP,失败,此列的值不是我重新赋值的值,疑惑

代码如下

            //如果该列有放TableDataI18nAttribute特性
            if (entityInfo.IsAnyAttribute<TableDataI18nAttribute>() && entityInfo.OperationType == DataFilterType.UpdateByObject)
            {
                #region 默认语言列不更新
                //获取当前要更新表的Type
                Type tableType = entityInfo.EntityValue.GetType();
                // 通过SqlSugar获取实体信息来找到主键列
                var entityDbInfo = db.EntityMaintenance.GetEntityInfo(tableType);
                var primaryKeyColumn = entityDbInfo.Columns.FirstOrDefault(c => c.IsPrimarykey);
                if (primaryKeyColumn != null)
                {
                    // 通过反射找到entityInfo.EntityValue中该属性的值
                    var primaryKeyProperty = tableType.GetProperty(primaryKeyColumn.PropertyName);
                    var primaryKeyValue = primaryKeyProperty?.GetValue(entityInfo.EntityValue);
                    if (primaryKeyValue != null)
                    {
                        var conModels = new List<IConditionalModel>();
                        conModels.Add(new ConditionalModel { FieldName = primaryKeyColumn.DbColumnName, ConditionalType = ConditionalType.Equal, FieldValue = primaryKeyValue.ToString() });
                        object? databaseRow = await db.QueryableByObject(tableType)
                         .Where(conModels)
                         .Select(entityInfo.EntityColumnInfo.DbColumnName)
                         .FirstAsync();
                        if (databaseRow != null)
                        {
                            //使用反射获取databaseRow中对应属性的值
                            PropertyInfo? propertyInfo = tableType.GetProperties().FirstOrDefault(p => p.Name == entityInfo.PropertyName);
                            if (propertyInfo != null)
                            {
                                object? databaseValue = propertyInfo.GetValue(databaseRow);
                                if (databaseValue != null)
                                      //第二次进入,我看entityInfo.EntityValue,也正常赋上值了,但是实际的更新语句没有走赋的值
                                    entityInfo.SetValue(databaseValue);
                            }
                        }
                    }
                }
                #endregion 默认语言列不更新
            }

希望,作者有时间的话,可以帮忙解答一下,十分感谢





热忱回答3