You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.6 KiB

2 years ago
  1. using OrmTest.Models;
  2. using OrmTest.UnitTest;
  3. using SqlSugar;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OrmTest
  10. {
  11. public class Delete : UnitTestBase
  12. {
  13. private Delete() { }
  14. public Delete(int eachCount)
  15. {
  16. this.Count = eachCount;
  17. }
  18. public void Init()
  19. {
  20. var db = GetInstance();
  21. //by entity
  22. var t1= db.Deleteable<Student>().Where(new Student() { Id = 1 }).ToSql();
  23. base.Check(@"DELETE FROM [STudent] WHERE [Id] IN ('1') ",
  24. null,
  25. t1.Key,
  26. null, "Delte t1 error"
  27. );
  28. //use lock
  29. var t2 = db.Deleteable<Student>().With(SqlWith.RowLock).ToSql();
  30. base.Check(@"DELETE FROM [STudent] WITH(ROWLOCK) ",
  31. null,
  32. t2.Key,
  33. null, "Delte t2 error"
  34. );
  35. //by primary key
  36. var t3 = db.Deleteable<Student>().In(1).ToSql();
  37. base.Check(@"DELETE FROM [STudent] WHERE [Id] IN ('1') ",
  38. null,
  39. t3.Key,
  40. null, "Delte tt error"
  41. );
  42. //by primary key array
  43. var t4 = db.Deleteable<Student>().In(new int[] { 1,2}).ToSql();
  44. base.Check(@"DELETE FROM [STudent] WHERE [Id] IN ('1','2') ", null, t4.Key, null, "Update t4 error");
  45. //by expression
  46. var t5 = db.Deleteable<Student>().Where(it=>it.Id==1).ToSql();
  47. base.Check(@"DELETE FROM [STudent] WHERE ( [ID] = @Id0 ) ", new List<SugarParameter>() {
  48. new SugarParameter("@Id0",1)
  49. }, t5.Key, t5.Value, "Delte t5 error");
  50. var t6 = db.Deleteable<Student>().Where("id=@id",new { id=1}).ToSql();
  51. base.Check(@"DELETE FROM [STudent] WHERE id=@id", new List<SugarParameter>() {
  52. new SugarParameter("@id",1)
  53. }, t6.Key, t6.Value, "Delte t6 error");
  54. var t7 = base.GetInstanceByAttribute().Deleteable<DeleteTestTable>().Where(new List<DeleteTestTable>() {
  55. new DeleteTestTable() { Id=1, Id2="x" },
  56. new DeleteTestTable() { Id=2, Id2="x1" }
  57. }).ToSql();
  58. base.Check("DELETE FROM [DeleteTestTable] WHERE (([Id]=N'1'AND [Id2]=N'x')OR ([Id]=N'2'AND [Id2]=N'x1')) ",null, t7.Key, null,
  59. "Delte t7 error");
  60. }
  61. }
  62. public class DeleteTestTable {
  63. [SugarColumn(IsPrimaryKey =true)]
  64. public int Id { get; set; }
  65. [SugarColumn(IsPrimaryKey = true)]
  66. public string Id2 { get; set; }
  67. }
  68. }