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.

69 lines
2.9 KiB

2 years ago
  1. using OrmTest.Models;
  2. using SqlSugar;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace OrmTest.UnitTest
  9. {
  10. public class Mapping : UnitTestBase
  11. {
  12. private Mapping() { }
  13. public Mapping(int eachCount)
  14. {
  15. this.Count = eachCount;
  16. }
  17. public void Init()
  18. {
  19. var db = GetInstance();
  20. var t1 = db.Queryable<Student>().Where(it => it.Id == 1).ToSql();
  21. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE ( [ID] = @Id0 ) ", null, t1.Key, null, "Mapping t1 error");
  22. db.MappingColumns.Add("Id", "id", "School");
  23. var t2 = db.Queryable<Student, School>((st, sc) => new object[] {
  24. JoinType.Left,st.SchoolId==sc.Id
  25. })
  26. .Where(st => st.Id == 1)
  27. .Where((st, sc) => sc.Id == 1)
  28. .Where((st, sc) => sc.Id == st.Id)
  29. .GroupBy(st => st.Id)
  30. .GroupBy((st, sc) => sc.Id).OrderBy(st => st.Id, OrderByType.Asc)
  31. .Select((st, sc) => new { stid = st.Id, scid = sc.Id }).ToSql();
  32. base.Check(@"SELECT [st].[ID] AS [stid] , [sc].[id] AS [scid] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[id] ) WHERE ( [st].[ID] = @Id0 ) AND ( [sc].[id] = @Id1 ) AND ( [sc].[id] = [st].[ID] )GROUP BY [st].[ID],[sc].[id] ORDER BY [st].[ID] ASC ",
  33. null, t2.Key, null, " Mapping t2 error");
  34. var x2 = GetInstance();
  35. var q = x2.Queryable<Student>().AS("t");
  36. var t3 = q.ToSql();
  37. var t4 = q.ToSql();
  38. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [t] ", null, t3.Key, null, "Mapping t3 error");
  39. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [t] ", null, t4.Key, null, "Mapping t3 error");
  40. var x3 = GetInstance2();
  41. x3.MappingTables.Add("Student", "studenT");
  42. int count = 0;
  43. var t5 = x3.Queryable<Student>().ToPageList(1,2,ref count);
  44. }
  45. public new SqlSugarClient GetInstance()
  46. {
  47. SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
  48. return db;
  49. }
  50. public SqlSugarClient GetInstance2()
  51. {
  52. SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
  53. db.Ado.IsEnableLogEvent = true;
  54. db.Ado.LogEventStarting = (sql, pars) =>
  55. {
  56. Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars));
  57. Console.WriteLine();
  58. };
  59. return db;
  60. }
  61. }
  62. }