diff --git a/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs b/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs index c00be0c33..b741dff72 100644 --- a/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs +++ b/Src/Asp.NetCore2/MongoDbTest/UnitTest/Cases.cs @@ -22,6 +22,7 @@ namespace MongoDbTest QueryJson5.Init(); QueryJson6.Init(); QueryJson7.Init(); + UnitJsonAndNew.Init(); QueryLeftJoin.Init(); QueryLeftJoin2.Init(); QueryLeftJoin3.Init(); diff --git a/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin2.cs b/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin2.cs index 319768767..ba234d7f1 100644 --- a/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin2.cs +++ b/Src/Asp.NetCore2/MongoDbTest/UnitTest/QueryLeftJoin2.cs @@ -99,7 +99,7 @@ namespace MongoDbTest studentName = s.Name, schoolName = sc.Name }).ToList(); - if (dt7.Count != 1 || dt7.Last().studentName != "李四" || dt7.Last().schoolName != "复旦大学") Cases.ThrowUnitError(); + if (dt7.Count != 2 || dt7.Last().studentName != "李四" || dt7.Last().schoolName != "复旦大学") Cases.ThrowUnitError(); } [SqlSugar.SugarTable("UnitStudentdu2s31")] public class Student : MongoDbBase diff --git a/Src/Asp.NetCore2/MongoDbTest/UnitTest/UnitJsonAndNew.cs b/Src/Asp.NetCore2/MongoDbTest/UnitTest/UnitJsonAndNew.cs new file mode 100644 index 000000000..570f04ae9 --- /dev/null +++ b/Src/Asp.NetCore2/MongoDbTest/UnitTest/UnitJsonAndNew.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace MongoDbTest +{ + + public class UnitJsonAndNew + + { + + public static void Init() + + { + + { + var db = DbHelper.GetNewDb(); + db.DbMaintenance.TruncateTable(); + + db.Insertable(new UserEntity { Id = 1, AccountName = "A", RoleIds = new List { 1 }, RoleId = 1 }).ExecuteCommand(); + db.Insertable(new UserEntity { Id = 2, AccountName = "B", RoleIds = new List { 3 }, RoleId = 1 }).ExecuteCommand(); + db.Insertable(new RolesEntity { Id = 1, Name = "存在" }).ExecuteCommand(); + db.Insertable(new RolesEntity { Id = 2, Name = "不存在" }).ExecuteCommand(); + + var query1 = db.Queryable() + + .InnerJoin((u, role) => u.RoleIds.Contains(role.Id))//多个条件用&& + + .Select((u, role) => new { u.AccountName, role.Name }) + + .ToList(); + + if (query1.Count != 1) Cases.ThrowUnitError(); + if (query1.First().Name != "存在") Cases.ThrowUnitError(); + var query2 = db.Queryable() + + .LeftJoin((u, role) => u.RoleIds.Contains(role.Id))//多个条件用&& + + .Select((u, role) => new { u.AccountName, role.Name }) + + .ToList(); + if (query2.Count != 2) Cases.ThrowUnitError(); + if (query2.First().Name!= "存在") Cases.ThrowUnitError(); + if (query2.Last().Name != null) Cases.ThrowUnitError(); + } + + } + + } + + + + /// + + /// 用户实体 + + /// + + [SugarTable("UserComponent")] + + public class UserEntity + + { + + /// + + /// 主键Id + + /// + + [SugarColumn(IsPrimaryKey = true, ColumnName = "_id")] + + public long Id { get; set; } + + + + /// + + /// 账户名。 + + /// + + public string? AccountName { get; set; } = null!; + + + + /// + + /// 角色ID列表 + + /// + + public long RoleId { get; set; } + + + + /// + + /// 角色ID列表 + + /// + + [SugarColumn(IsJson = true)] + + public List RoleIds { get; set; } + + } + + [SugarTable("RoleComponent")] + + public class RolesEntity + + { + + /// + + /// 主键Id + + /// + + [SugarColumn(IsPrimaryKey = true, ColumnName = "_id")] + + public long Id { get; set; } + + public string? Name { get; set; } + + } +}