导航删除

请安装:预览版本 5.0.9.3-previe05

与EF CORE区别

SqlSugar不依赖数据库外键,只要配置实体导航就能使用


1、导航删除语法(多层级)

设计参考于EF Core查询,只要配置好实体就可以随意使用导航进行导航删除操作

 db.DeleteNav<Student>(it=>it.Id==1)//删除主表 Student(id=1)
     .Include(z1 => z1.SchoolA) .ThenInclude(z1 => z1.RoomList) //删除2层 Root->ShoolA->RoomList
     .Include(z1 => z1.Books)// 删除1层 Root->Books
     .ExecuteCommand();

 //也可能根据集合删除,不需有子对象         
 List<Student> list=db.Queryable<Student>().Where(it=>it.id==1).ToList();     
 db.DeleteNav<Student>()//删除主表 Student(id=1)
     .Include(z1 => z1.SchoolA) .ThenInclude(z1 => z1.RoomList) //删除2层 Root->ShoolA->RoomList
     .Include(z1 => z1.Books)// 删除1层 Root->Books
     .ExecuteCommand()
     

//Include表式从第一层开始,ThenInclude表式在上一层继续向下
Include(z1 => z1.A) .ThenInclude(z1 => z1.B).ThenInclude(z1 => z1.C) 
Include(z1 => z1.A) .ThenInclude(z1 => z1.B).ThenInclude(z1 => z1.D)


2、一对一

 2.1 逻辑

先删子表,在删主表

 2.2 用例

//实体
public class StudentA
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int StudentId { get; set; }
    public string Name { get; set; }
    public int SchoolId { get; set; }
    //标准配置 推荐
    [Navigate(NavigateType.OneToOne, nameof(SchoolId))]//一对一 SchoolId是StudentA类里面的
    public SchoolA SchoolA { get; set; } //不能赋值只能是null
      

 
}
public class SchoolA
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int Id{ get; set; }
    public string SchoolName { get; set; }
}

//代码 :先删SchoolA在删StudentA
db.DeleteNav<StudentA>(it=>it.Id==1)
            .Include(z1 => z1.SchoolA ) 
            .ExecuteCommand();


3、一对多

 3.1 逻辑

先删子表,在删主表

 3.2 用例

//实体
public class StudentA
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int Id{ get; set; }
    public string Name { get; set; }
    public int SchoolId { get; set; }
    [Navigate(NavigateType.OneToMany, nameof(BookA.studenId))]//BookA表中的studenId
    public List<BookA> Books { get; set; }//注意禁止给books手动赋值
 
 
}
public class BookA
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int BookId { get; set; }
    public string Name { get; set; }
    public int studenId { get; set; }
}

//代码
db.DeleteNav<StudentA>(it=>it.Id==1)
            .Include(z1 => z1.Books) 
            .ExecuteCommand();

4、多对多

 4.1 实体配置

//中间表
public class ABMapping1
{
    [SugarColumn(IsPrimaryKey = true)]//中间表可以不是主键
    public int AId { get; set; }
    [SugarColumn(IsPrimaryKey = true)]//中间表可以不是主键
    public int BId { get; set; }
}
public class A1
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int Id { get; set; }
    public string Name { get; set; }
    [Navigate(typeof(ABMapping1), nameof(ABMapping1.AId), nameof(ABMapping1.BId))]//注意顺序
    public List<B1> BList { get; set; }//只能是null不能赋默认值
}
public class B1
{
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int Id { get; set; }
    public string Name { get; set; }
    [Navigat(typeof(ABMapping1), nameof(ABMapping1.BId), nameof(ABMapping1.AId))]//注意顺序
    public List<A1> AList { get; set; }//只能是null不能赋默认值
}



A表代表主表,B表代表从表, 中间表 是A和B的关系表

 4.2 只删中间表(1张表)

//只删除关系
db.DeleteNav<UserInfo>(x=>x.id==1)
				.Include(x => x.Roles) // Roles我们称为B表
				.ExecuteCommand();

 4.3 删A表和中间表(2张表)

//比如用户、角色、和中间表  这个操作一般是删除用户(因为删用户不删关系存在脏数据)
db.DeleteNav<UserInfo>(x=>x.id==1)
				.Include(x => x.Roles,new DeleteNavOptions() { 
					ManyToManyIsDeleteA=true 
				 })
				.ExecuteCommand();

 4.4 删B表和中间表(2张表)

db.DeleteNav<UserInfo>(x=>x.id== list4.First().id)
				.Include(x => x.Roles,new DeleteNavOptions() { 
					ManyToManyIsDeleteB=true 
				 })
				.ExecuteCommand();

 4.5 删A、B和中间表(3张表)

db.DeleteNav<UserInfo>(x=>x.id== 1)
				.Include(x => x.Roles,new DeleteNavOptions() { 
					ManyToManyIsDeleteB=true,
					ManyToManyIsDeleteA=true 
				 })
				.ExecuteCommand();

5、更多导航操作

导航更新: https://www.donet5.com/home/Doc?typeId=2432

导航删除: https://www.donet5.com/home/Doc?typeId=2431

导航插入: https://www.donet5.com/home/Doc?typeId=2430

导航查询: https://www.donet5.com/home/Doc?typeId=1188


关闭
文档:SqlSugar5.0