删除

1、根据实体删除,需要配置主键 

根据主键删除需要给实体配置主键,参考文档实体配置

db.Deleteable<Student>().Where(new Student() { Id = 1 }).ExecuteCommand();


2、根据主键

db.Deleteable<Student>().In(1).ExecuteCommand();


3、根据主键数组

db.Deleteable<Student>().In(new int[] { 1, 2 }).ExecuteCommand();


4、根据表达式

db.Deleteable<Student>().Where(it => it.Id == 1).ExecuteCommand();


5、子查询删除

db.Deleteable<Student>().Where(p => p.SchoolId == SqlFunc.Subqueryable<School>().Where(s => s.Id == p.SchoolId).Select(s => s.Id)).ExecuteCommand()


6、表达式偷懒

db.Deleteable<Student>(new Student() { Id = 1 }).ExecuteCommand();


7、封装假删除

比如 var rep=new Repository<Order>();

rep.FalseDelete(it=>it.id==10); 其中Order类需要继承 IDeleted

   public  interface IDeleted  {
         bool IsDeleted { get; set; }
    }
   
    public class Repository<T> : SimpleClient<T> where T : class, IDeleted, new()
    {
        public Repository(ISqlSugarClient context = null) : base(context)
        {
            if (context == null)
            {
                base.Context = new SqlSugarClient(new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.PostgreSQL,
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                    ConnectionString = Config.ConnectionString,
                    AopEvents = new AopEvents
                    {
                        OnLogExecuting = (sql, p) =>
                        {
                            Console.WriteLine(sql);//调式SQL
                        }
                    }
                });
            }
        }

        /// <summary>
        /// 这样封装就完成了一个通用假删除
        /// </summary>
        /// <returns></returns>
        public bool FalseDelete(Expression<Func<T, bool>> expression)
        {
            return Context.Updateable<T>()
                    .Where(expression)
                    .SetColumns(it => it.IsDeleted == true).ExecuteCommand()>0 ;
        }
    }