关于实体与数据库不一致时Select表达式的Bug(AS前后颠倒) 返回

反馈
6 1574
该叫什么 hdz? 发布于2018/1/22
悬赏:5 飞吻


由于某种原因,需要实体字段和数据库字段不一致处理,采用动态Mapping的方法,会导致AS前后颠倒,出现问题的代码如下:


conn.Queryable<User>()

    .Where(p => p.UserLevel <= level)
    .Select(p => new { data = p }).ToSql();


得到如下SQL:

SELECT  `UserId` AS `User.D7AD72DE250DF8D873979DA288C705EB1` , `UserName` AS `User.D2EA2758549D756D9FE8078B6770FB498` , `UserPass` AS `User.D09B061FC8EFEE1F3E611018668140662` , `UserSex` AS `User.D4EBF74B96CBE45BF91A13381F05B3DE1` , `UserPhone` AS `User.D176FE0BCC9E378C2A5FB39B931D7FE41` , `UserLevel` AS `User.D353C3CA6E6CC1DF5008F0C351537639F` , `CreateDate` AS `User.DCC91194A8E27F7EDE49B5280FB0E7C9D`  FROM `User`  WHERE ( `D353C3CA6E6CC1DF5008F0C351537639F` <= @UserLevel0 )



--------------

把字段罗一一列出来时,则得到的SQL正确

conn.Queryable<User>()

    .Where(p => p.UserLevel <= level)
    .Select(p => new { p.UserId, p.UserLevel, p.UserName, p.UserPhone }).ToSql();


得到如下SQL:

SELECT  `D7AD72DE250DF8D873979DA288C705EB1` AS `UserId` , `D353C3CA6E6CC1DF5008F0C351537639F` AS `UserLevel` , `D2EA2758549D756D9FE8078B6770FB498` AS `UserName` , `D176FE0BCC9E378C2A5FB39B931D7FE41` AS `UserPhone`  FROM `User`  WHERE ( `D353C3CA6E6CC1DF5008F0C351537639F` <= @UserLevel0 )


热忱回答6

  • 这个BUG 最新版本解决了,如果是CORE版本等今天更新一下

    0 回复
  • 如果是最新版本,动态mapping应该 是你mapping参数传反了吧

    0 回复
  • hdz? hdz? VIP0
    2018/1/22

    是最新版,mapping参数没传反,两种写法,一种正常,另一种是反的,说明对表达式的解析有一个有问题

    0 回复
  • 最好有代码,我认为是传反了引起的

    0 回复
  • hdz? hdz? VIP0
    2018/1/23

    @fate stay night

    //测试代码

    using (var conn = Ekong.Common.Data.Config.DataHandler["config"].UsingMapping(typeof(User)))
                    {
                        var level = Monitors.Config.One.UserLeveal;
                        var sql = conn.Queryable<User>().Where(p => p.UserLevel <= level)
                            .Select(p => new
                            {
                                data = p
                            }).ToSql();
                    }


    //得到的SQL

    SELECT  `UserId` AS `User.D7AD72DE250DF8D873979DA288C705EB1` , `UserName` AS `User.D2EA2758549D756D9FE8078B6770FB498` , `UserPass` AS `User.D09B061FC8EFEE1F3E611018668140662` , `UserSex` AS `User.D4EBF74B96CBE45BF91A13381F05B3DE1` , `UserPhone` AS `User.D176FE0BCC9E378C2A5FB39B931D7FE41` , `UserLevel` AS `User.D353C3CA6E6CC1DF5008F0C351537639F` , `CreateDate` AS `User.DCC91194A8E27F7EDE49B5280FB0E7C9D` , `Guid` AS `User.D54307C46C6A41A0827F196BA3763814F`  FROM `User`  WHERE ( `D353C3CA6E6CC1DF5008F0C351537639F` <= @UserLevel0 )


    //数据库结构image.png


    //用到的扩展函数

    public static SqlSugarClient UsingMapping(this SqlSugarClientExtend client, bool useMd5 = true,
                params object[] types)
            {
                if (types != null && types.Length > 0)
                {
                    for (int i = 0; i < types.Length; i += 2)
                    {
                        //得到类型
                        var entityType = types[i] as Type;


                        //绑定字段名
                        if (useMd5)
                        {
                            //获取真实表格
                            var tableName = types.Length > i + 1 ? types[i + 1] as string : null;


                            //绑定真实表格
                            if (!string.IsNullOrEmpty(tableName))
                                client.MappingTables.Add(entityType.Name, tableName);


                            //绑定真实字段
                            foreach (var field in entityType.GetPropertyInfos())
                                client.MappingColumns.Add(field.Name, "D" + Md5.Md5Field(field.Name), entityType.Name);
                        }


                        //获取表名
                        var table = client.Context.EntityMaintenance.GetTableName(entityType);
                        var model = IDataModel.GetInstance(entityType);


                        //初始化
                        if (!model.GetHasTable(client.DbName + table))
                        {
                            model.SetHasTable(client.DbName + table,
                                client.DbMaintenance.IsAnyTable(table));
                            if (!model.GetHasTable(client.DbName + table))
                            {
                                //修改连接方式
                                client.CurrentConnectionConfig.InitKeyType = InitKeyType.Attribute;


                                //初始化数据表
                                client.CodeFirst.InitTables(entityType);


                                //添加索引
                                AddIndexs(client, client.Context.EntityMaintenance.GetTableName(entityType), entityType
                                    .GetCustomAttributes<SugarColumnExtend>()
                                    .Where(p => p.Value.IsIndex)
                                    .Select(p => new
                                    {
                                        p.Value.IndexName,
                                        p.Value.IndexType,
                                        ColumnName =
                                        useMd5 ? "D" + Md5.Md5Field(p.Key.Name) : (p.Value.ColumnName ?? p.Key.Name)
                                    })
                                    .GroupBy(p => p.IndexName)
                                    .ToDictionary(p => p.Key,
                                        p => p.Select(x =>
                                                new SugarColumnExtend {ColumnName = x.ColumnName, IndexType = x.IndexType})
                                            .ToList())
                                );


                                //修改引擎
                                ChangeEngine(client, entityType, table);


                                //初始化
                                var defModels = model.GetDefault();
                                model.InitModel<IDataModel>(client, defModels);


                                //值数据表存在状态
                                model.SetHasTable(client.DbName + table, true);
                            }
                        }
                    }
                }


                return client;
            }


    0 回复
  • 好的我看一下

    0 回复

版块

学习文档

最新会员

发布达人

回贴达人