FreeSql icon indicating copy to clipboard operation
FreeSql copied to clipboard

动态表查询WhereIf参数识别不到

Open xiahanlin opened this issue 4 months ago • 16 comments

Image 提示没有taskno这个参数,实际代码已经添加了参数。

xiahanlin avatar Aug 19 '25 09:08 xiahanlin

mysql 试试 ?taskno @taskno

2881099 avatar Aug 19 '25 13:08 2881099

是mysql . dic["@taskno"] 这样也试过不行。 最开始我是new { taskno = "xxx"} 。

xiahanlin avatar Aug 20 '25 01:08 xiahanlin

.Where("TaskNo = @taskno", new { taskno = "xxx" }) .Where("TaskNo = ?taskno", new { taskno = "xxx" })

测试下面代码会不会触发

if (obj is IDictionary<string, object> dic1)
{
    foreach (var key in dic1.Keys)
    {
        var dbkey = key.TrimStart('@', '?', ':');
        if (isCheckSql && string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{dbkey}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
        var val = dic1[key];
        var valType = val == null ? typeof(string) : val.GetType();
        if (ttype.IsAssignableFrom(valType)) ret.Add((T)val);
        else ret.Add(constructorParamter(dbkey, valType, val));
    }
}
else if (obj is IDictionary dic2)
{
    foreach (var key in dic2.Keys)
    {
        var dbkey = key.ToString().TrimStart('@', '?', ':');
        if (isCheckSql && string.IsNullOrEmpty(paramPrefix) == false && sql.IndexOf($"{paramPrefix}{dbkey}", StringComparison.CurrentCultureIgnoreCase) == -1) continue;
        var val = dic2[key];
        var valType = val == null ? typeof(string) : val.GetType();
        if (ttype.IsAssignableFrom(valType)) ret.Add((T)val);
        else ret.Add(constructorParamter(dbkey, valType, val));
    }
}

2881099 avatar Aug 20 '25 01:08 2881099

这个代码我咋测呢。直接用.where 不用where if 我也试过了不行。 我现在已经提交了bug.能复现的话就修复啦。

xiahanlin avatar Aug 21 '25 01:08 xiahanlin

写一个方法,把你的字典传给方法,debug

2881099 avatar Aug 21 '25 06:08 2881099

我的测试结果就是动态表加where(sql,params)无效。你验证一下不就行了。

xiahanlin avatar Aug 25 '25 04:08 xiahanlin

这是开源项目,不是只接受问题反馈。我没有你的环境和代码,没办法帮你验证。

2881099 avatar Aug 25 '25 06:08 2881099

这个并不是什么特殊的和系统相关的。就是你的那个方法,你执行一下,行就是行,不行就是不行。你自己都没验证过你的行不行,就让别人去验证。

xiahanlin avatar Aug 25 '25 07:08 xiahanlin

你要是说是环境问题,那你就说你的环境行不行。 至少你先证明你自己的没问题。

xiahanlin avatar Aug 25 '25 07:08 xiahanlin

凭什么是我证明没有问题?如果我的写法没问题,你的写法有问题,那我得到了什么?

这个功能多少年了,你来说有问题,还要我证明。

这是开源社区,不是专门为你服务的专场。

2881099 avatar Aug 25 '25 10:08 2881099

动态表。不是常用的查询。可能动态表用的不多。 总之我反馈也是好意。不要感觉是别人在针对你。 而且我也截图了。就是按你动态表的写法调用的查询拼接的Where。不要说什么这个功能多少年这种自满的话。 我前段时间用你的aop全局映射枚举map转类型还出错了。我改到字段上映射了。

xiahanlin avatar Aug 25 '25 11:08 xiahanlin

虽然freesql常规orm确实挺好用的 。但是谁一天这么无聊去给你留言。社区提反馈难道不是让社区作品变的更好吗。提个issue还整成专场了 ,张口闭口开源社区。 我提bug,只是说有这个bug.改不改是你的事。或者参与代码贡献的人。这里仅仅是反馈问题,你能回复有这个问题或者没这个问题,这就行了。 扯那么多做什么。很简单的事情还上升到讨论哲学。

xiahanlin avatar Aug 25 '25 11:08 xiahanlin

using FreeSql.DataAnnotations; using FreeSql.Internal.Model; using System.Diagnostics;

namespace freesqlTest { internal class Program { static void Main(string[] args) { string connectStr = "Data Source=localhost;port=3306;User ID=root;Password=root;Database=testdb;CharSet=utf8;sslmode=none;AllowPublicKeyRetrieval=True";

        //初始化FreeSql 
        IFreeSql fsql = new FreeSql.FreeSqlBuilder()
                .UseConnectionString(FreeSql.DataType.MySql, connectStr)
                .UseAdoConnectionPool(true)
                .UseMonitorCommand(cmd => Debug.WriteLine($"Sql:{cmd.CommandText}"))
                .UseAutoSyncStructure(true) //自动同步实体结构到数据库,只有CRUD时才会生成表
                .Build();
        fsql.Aop.ConfigEntityProperty += (s, e) =>
        {
            if (e.Property.PropertyType == typeof(decimal) || e.Property.PropertyType == typeof(decimal?))
            {
                e.ModifyResult.Precision = 18;
                e.ModifyResult.Scale = 6;
            }
        };

        //添加动态表
        var table = GetFsqlTableInfo(fsql);
        fsql.CodeFirst.SyncStructure(table.Type); //创建表
       
        try
        {
            //这里不需要插入数据,直接进行查询方法测试
            var datas1 = fsql.Select<object>().AsType(table.Type).Where("Id > 1").ToList();
            //上面语句测试通过
            //下面语句仅仅加入参数测试无法通过

            var datas2 = fsql.Select<object>().AsType(table.Type).Where("Id > @id", new { id = 10 }).ToList();
        }
        catch (Exception ex)
        {

            throw;
        }

    }



    private static TableInfo GetFsqlTableInfo(IFreeSql fsql)
    {
        string TableName = "test_table";
        var tableName = $"{TableName}";
        List<Attribute> attributes = new List<Attribute>();
        attributes.Add(new TableAttribute { Name = tableName });
        
        var tableBuilder = fsql.CodeFirst.DynamicEntity(tableName,
            attributes.ToArray()
            );
        tableBuilder.Property("Id", typeof(long), new ColumnAttribute() { IsIdentity = true, IsPrimary = true });
        tableBuilder.Property("Field1", typeof(string), new ColumnAttribute() { StringLength = 50 });
       

        tableBuilder.Property("CreatedTime", typeof(DateTime));
        tableBuilder.Property("Nullity", typeof(bool)); //是否禁用
        tableBuilder.Property("IsDeleted", typeof(bool)); //是否软删除
        var table = tableBuilder.Build();
        return table;
    }
}

}

xiahanlin avatar Aug 25 '25 11:08 xiahanlin

.Where("Id > ?id", new { id = 10 })

二楼就回复过了

2881099 avatar Aug 26 '25 00:08 2881099

哦 2楼我可能理解错了。 你最新的回复用例可以了 。 谢谢。 另外我的写法是参考的文档或者注释。

Image

xiahanlin avatar Aug 26 '25 05:08 xiahanlin

使用FreeSql.Provider.MySqlConnector,使用@前缀生效 使用FreeSql.Provider.MySql, 使用?前缀生效

xiahanlin avatar Aug 26 '25 05:08 xiahanlin