SqlSugar/Src/Asp.NetCore2/MongoDbTest/UnitTest/Update.cs

55 lines
2.1 KiB
C#
Raw Normal View History

2025-06-20 15:01:27 +08:00
using SqlSugar.MongoDb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MongoDbTest
{
public class Update
{
internal static void Init()
{
var db = DBHelper.DbHelper.GetNewDb();
db.CodeFirst.InitTables<Student>();
db.DbMaintenance.TruncateTable<Student>();
2025-06-20 16:35:00 +08:00
db.Insertable(new Student() { Age = 1, Name = "11", SchoolId = "111", CreateDateTime = DateTime.Now }).ExecuteCommandAsync().GetAwaiter().GetResult();
db.Insertable(new List<Student>() {
new Student() { Age = 2, Name = "22", SchoolId = "222", CreateDateTime = DateTime.Now },
new Student() { Age = 3, Name = "33", SchoolId = "333", CreateDateTime = DateTime.Now }
}).ExecuteCommandAsync().GetAwaiter().GetResult();
var list=db.Queryable<Student>().ToList();
db.Insertable(new Student() { Age = 1, Name = "xx", SchoolId = "111", CreateDateTime = DateTime.Now }).ExecuteCommandAsync().GetAwaiter().GetResult();
foreach (var item in list)
{
item.Name = item.Name + "haha";
}
db.Updateable(list).ExecuteCommand();
var list2 = db.Queryable<Student>().ToList();
foreach (var item in list2)
{
if (item.Name == "xx") continue;
if (!item.Name.EndsWith("haha")) Cases.ThrowUnitError();
}
2025-06-20 16:38:06 +08:00
db.Updateable<Student>()
.SetColumns(it => new Student() { Name = "yy" }).Where(it => it.Name == "xx").ExecuteCommand();
var datas= db.Queryable<Student>().Where(it => it.Id == list2.Last().Id).ToList();
if(datas.Count!=1|| datas.First().Name!="yy") Cases.ThrowUnitError();
2025-06-20 15:01:27 +08:00
}
[SqlSugar.SugarTable("UnitStudentdghhuesd3z1")]
public class Student : MongoDbBase
{
public string Name { get; set; }
public string SchoolId { get; set; }
public int Age { get; set; }
public DateTime CreateDateTime { get; set; }
}
}
}