多对多导航删除出错了 返回

SqlSugar 老数据
13 408
//多对多关系
            {
                if (db.DbMaintenance.IsAnyTable("User", false))
                {
                    db.DbMaintenance.DropTable<User>();
                }
                if (db.DbMaintenance.IsAnyTable("Role", false))
                {
                    db.DbMaintenance.DropTable<Role>();
                }
                if (db.DbMaintenance.IsAnyTable("UserRoleMapping", false))
                {
                    db.DbMaintenance.DropTable<UserRoleMapping>();
                }
                db.CodeFirst.InitTables<User>();
                db.CodeFirst.InitTables<Role>();
                db.CodeFirst.InitTables<UserRoleMapping>();
                List<User> users = new List<User>()
                {
                    new User()
                    {
                        Id = 1,
                        CompanyId = 1,
                        CreateTime = DateTime.Now,
                        UserName = "Richard老师",
                        RoleList = new List<Role>()
                        {
                            new Role()
                            {
                                Id = 1,
                                CreateTime = DateTime.Now,
                                RoleName = "管理员"
                            },
                            new Role()
                            {
                                Id = 2,
                                CreateTime= DateTime.Now,
                                RoleName="金牌讲师"
                            }
                        }
                    }
                };
                // 插入第一层 SchoolA 
                db.InsertNav(users)
                    .Include(u => u.RoleList)
                    .ExecuteCommand();
                var list1 = db.Queryable<User>()
                    .Includes(x => x.RoleList);
                var list2 = db.Queryable<User>()
                    .Includes(x => x.RoleList.Where(r => r.RoleName == "管理员").ToList())
                    .Where(c => c.UserName.Equals("Richard老师"))
                    .ToList();
                bool bResult = db.UpdateNav(list2)
                    .Include(u => u.RoleList)
                    .ExecuteCommand();
                bool bResult1 = db.DeleteNav(list1)//程序报错
                    .Include(u => u.RoleList)
                    .ExecuteCommand(); ;
                int iResult1 = db.Deleteable(list1).ExecuteCommand();//程序报错
                var roles = db.Queryable<Role>()
                    .ToList();
                int iResult2 = db.Deleteable(roles).ExecuteCommand();
            }


其中,注释地方的代码,VS报错了。


错误CS0310“ISugarQueryable<User>”必须是具有公共的无参数构造函数的非抽象类型,
才能用作泛型类型或方法“SqlSugarClient.DeleteNav<T>(T)”中的参数“T”
SQLSugarPostgreSQLE:\VS\source\repos\SQLSugarPostgreSQL\SQLSugarPostgreSQL\ViewModels\QueryOperationInfo.cs925活动

错误CS0310“ISugarQueryable<User>”必须是具有公共的无参数构造函数的非抽象类型,
才能用作泛型类型或方法“SqlSugarClient.Deleteable<T>(T)”中的参数“T”
SQLSugarPostgreSQLE:\VS\source\repos\SQLSugarPostgreSQL\SQLSugarPostgreSQL\ViewModels\QueryOperationInfo.cs929活动


热忱回答13

  • fate sta fate sta VIP0
    2023/3/12

    实体提供一下

    0 回复
  • fate sta fate sta VIP0
    2023/3/12

    int [] ids=list.xselect(it=>it.id).ToList();           

    db.DeleteNav<ScheduleEntity>(x =>Ids.Contains(x.id))

                                .Include(x =>x.xxxx).ToList())  


    你先这样试试

       

    0 回复
  • fate sta fate sta VIP0
    2023/3/12

    image.png

    未重现你的问题,需要提供一下完整测试用例,并且你先本地测试一下

    0 回复
  • fate sta fate sta VIP0
    2023/3/12

    “SqlSugarClient.DeleteNav<T>(T)”中的参数“T”  你这个错一般是封装泛型没加new()引起的,是不是有第三方的封装操作

    0 回复
  • using SqlSugar;
    
    namespace SQLSugarPostgreSQL.Models
    {
        [SugarTable("User")]// 
        public class User
        {
            [SugarColumn(IsPrimaryKey = true/*, IsIdentity = true*/)]//数据库是自增才配自增 
            public int Id { get; set; }
    
            [SugarColumn(ColumnName = "CompanyId", IsNullable = true)]
            public int? CompanyId { get; set; }
    
            [Navigate(NavigateType.ManyToOne, nameof(CompanyId))]
            public Company? CompanyInfo { get; set; }
    
            [SugarColumn(ColumnName = "UserName", IsNullable = true)]//数据库与实体不一样设置列名 
            public string? UserName { get; set; }
    
            [SugarColumn(ColumnName = "CreateTime", IsNullable = true)]
            public DateTime? CreateTime { get; set; }
    
            public int UserDetailId { get; set; }
    
            [Navigate(NavigateType.OneToOne, nameof(UserDetailId))]
            public UserDetail? UserDetailInfo { get; set; } //不能赋值只能是null
    
            [Navigate(typeof(UserRoleMapping), nameof(UserRoleMapping.UserId), nameof(UserRoleMapping.RoleId))]//注意顺序
            public List<Role> RoleList { get; set; } //不能赋值只能是null
        }
        
        [SugarTable("Role")]// 
        public class Role
        {
            [SugarColumn(IsPrimaryKey = true/*, IsIdentity = true*/)]
            public int Id { get; set; }
    
            [SugarColumn(ColumnName = "RoleName", IsNullable = true)]
            public string? RoleName { get; set; }
    
            [SugarColumn(ColumnName = "CreateTime", IsNullable = true)]
            public DateTime? CreateTime { get; set; }
    
            [Navigate(typeof(UserRoleMapping), nameof(UserRoleMapping.RoleId), nameof(UserRoleMapping.UserId))]//注意顺序
            public List<User>? UserList { get; set; } //不能赋值只能是null
        }
        
        [SugarTable("UserRoleMapping")]// 
        public class UserRoleMapping
        {
            [SugarColumn(IsPrimaryKey = true)]//中间表可以不是主键
            public int UserId { get; set; }
    
            [SugarColumn(IsPrimaryKey = true)]//中间表可以不是主键
            public int RoleId { get; set; }
        }

    以上是数据库表的实体。

    0 回复
  • fate sta fate sta VIP0
    2023/3/12

    image.png

    0 回复
  • fate sta fate sta VIP0
    2023/3/12

    你的代码跑过吗,编译都不能通过

    0 回复
  • @fate sta:就是编译不能通过,我也不知道是什么原因。我是抄的教程视频中的例子。

    0 回复
  • fate sta fate sta VIP0
    2023/3/12

     要传List<T> 你传的是Queryable

    0 回复
  • 但视频中是可以编译通过的。

    0 回复
  • fate sta fate sta VIP0
    2023/3/12
    var list=db.Queryable<XXX>().ToList(); 这样在删除
    0 回复
  • fate sta fate sta VIP0
    2023/3/12

    以文档为准

    0 回复
  • 谢谢,现在可以编译了。

    0 回复