Skip to content
果糖网 edited this page Nov 26, 2025 · 6 revisions
//by entity
db.Deleteable<Student>().Where(new Student() { Id = 1 }).ExecuteCommand();

//by primary key
db.Deleteable<Student>().In(1).ExecuteCommand();

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

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

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

Can be simplified.

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

Soft Delete

    //Three constraints must not be missing  【where T:class,IDeleted,new()】
    public void FalseDelete<T>(Expression<Func<T,bool>> exp) where T:class,IDeleted,new()
    {
        db.Updateable<T>()
                .SetColumns(it => new T() {  IsDeleted = true },
                 true)//true supports updating the data filter assignment field together
                .Where(exp).ExecuteCommand();
    }
     
    //建一个接口,继承这个接口的类就能用FalseDelete
    public interface IDeleted
    {
        bool IsDeleted { get; set; }
    }

Clone this wiki locally