db.Deleteable<Model>()方法中使用联表子查询时生成的SQL语句报错 返回

SqlSugar 处理中
187
该叫什么 MRPG 发布于1个月前
悬赏:0 飞吻

image.png

生成的语句:

DELETE FROM [Model] WHERE  [id]  in (SELECT [f1].[id] FROM ( SELECT * FROM [Model])   [f1]  WITH(NOLOCK)    INNER JOIN ( SELECT * FROM [Model])   [f2]  WITH(NOLOCK)  ON ( [f1].[id] = [f2].[id] ) WHERE (( [f1].[name] = '1' ) AND ( [f2].[name] = '2' )) GROUP BY [f1].[id])  


上面的红色的地方换成下面的可以执行:

DELETE FROM [Model] WHERE  [id]  in (SELECT [f1].[id] FROM [Model]  [f1]  WITH(NOLOCK)    INNER JOIN [Model]   [f2]  WITH(NOLOCK)  ON ( [f1].[id] = [f2].[id] ) WHERE (( [f1].[name] = '1' ) AND ( [f2].[name] = '2' )) GROUP BY [f1].[id])  


生成代码:

using SqlSugar;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using DAL;


namespace ConsoleApp1

{

    internal class Program

    {

        public class Model

        {

            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]

            public int id { set; get; }

            public string name { set; get; }

        }

        static void Main(string[] args)

        {

            var db = new DBHelper().GetDb();

            var deleteable = db.Queryable<Model>().Where(f => f.id == SqlFunc.Subqueryable<Model>().InnerJoin<Model>((f1, f2) => f1.id == f2.id).Where((f1, f2) => f1.name == "1" && f2.name == "2").GroupBy((f1, f2) => f1.id).Select((f1, f2) => f1.id));

            var sql = deleteable.ToSqlString();

            var res = deleteable.ExecuteCommand();

        }

    }

}

数据库连接配置:

using SqlSugar;

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Linq;

using System.Web;


namespace DAL

{

    public class DBHelper

    {

        public SqlSugarClient GetDb()

        {

            return new SqlSugarClient(new ConnectionConfig()

            {

                ConnectionString = ConfigurationManager.AppSettings["ConnectionString"],//连接符字串

                DbType = DbType.SqlServer,//数据库类型

                IsAutoCloseConnection = true, //不设成true要手动close

                MoreSettings = new ConnMoreSettings()

                {

                    DisableNvarchar = true,//添加这一行 ,将参数全部转成varchar模式

                    IsAutoRemoveDataCache = true,//增删改自动清除缓存,子查询或者Mergetable缓存的除外

                    IsWithNoLockQuery = true,//全局查询无锁

                    EnableModelFuncMappingColumn = true,

                    IsWithNoLockSubquery = true

                }

            }

            ,

            db =>

            {

            //SQL执行完

            db.Aop.OnLogExecuted = (sql, pars) =>

            {

            };

                db.Aop.DataExecuting = (oldValue, entityInfo) =>

                {

                    

                };

                db.Aop.DataExecuted = (value, entity) =>

                {

                };

            }

        );

        }

    }

}

上面的db.Deleteable<Model>()换成db.Queryable<Model>()生成的语句是正确的

SqlSugar版本:

image.png

.Net版本:4.6.1

热忱回答0