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.

64 lines
2.3 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.Demo
  9. {
  10. public class Insert:DemoBase
  11. {
  12. public static void Init()
  13. {
  14. var db = GetInstance();
  15. db.IgnoreColumns.Add("TestId", "Student");
  16. var insertObj = new Student() { Name = "jack", CreateTime = Convert.ToDateTime("2010-1-1"), SchoolId = 1 };
  17. //Insert reutrn Insert Count
  18. var t2 = db.Insertable(insertObj).ExecuteCommand();
  19. //Insert reutrn Identity Value
  20. var t3 = db.Insertable(insertObj).ExecuteReturnIdentity();
  21. //Insert reutrn Identity Value
  22. var t31 = db.Insertable(insertObj).ExecuteReturnEntity();
  23. //Only insert Name and SchoolId
  24. var t4 = db.Insertable(insertObj).InsertColumns(it => new { it.Name, it.SchoolId }).ExecuteReturnIdentity();
  25. var t4_1 = db.Insertable(insertObj).InsertColumns(it => it=="Name"||it== "SchoolId").ExecuteReturnIdentity();
  26. //Ignore TestId
  27. var t5 = db.Insertable(insertObj).IgnoreColumns(it => new { it.Name, it.TestId }).ExecuteReturnIdentity();
  28. //Ignore TestId
  29. var t6 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReturnIdentity();
  30. //Use Lock
  31. var t8 = db.Insertable(insertObj).With(SqlWith.UpdLock).ExecuteCommand();
  32. var insertObj2 = new Student() { Name = null, CreateTime = Convert.ToDateTime("2010-1-1") };
  33. var t9 = db.Insertable(insertObj2).Where(true/* Is insert null */, false/*off identity*/).ExecuteCommand();
  34. //Insert List<T>
  35. var insertObjs = new List<Student>();
  36. for (int i = 0; i < 1000; i++)
  37. {
  38. insertObjs.Add(new Student() { Name = "name" + i });
  39. }
  40. var t10 = db.Insertable(insertObjs.ToArray()).InsertColumns(it => new { it.Name }).ExecuteCommand();
  41. var t11 = db.Insertable(insertObjs.ToArray()).ExecuteCommand();
  42. var t12 = db.Insertable(insertObj).IgnoreColumns(it => it == "Name" || it == "TestId").ExecuteReturnIdentityAsync();
  43. t12.Wait();
  44. }
  45. }
  46. }