DataSource.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using Vitorm.Sql;
  2. using Vit.Extensions;
  3. using Vit.Core.Util.ConfigurationManager;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. namespace Vitorm.MsTest
  6. {
  7. [System.ComponentModel.DataAnnotations.Schema.Table("User", Schema = "dbo")]
  8. public class User
  9. {
  10. [System.ComponentModel.DataAnnotations.Key]
  11. [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  12. [System.ComponentModel.DataAnnotations.Schema.Column("id", TypeName = "int")]
  13. public int id { get; set; }
  14. [System.ComponentModel.DataAnnotations.Schema.Column("name", TypeName = "varchar(1000)")]
  15. [System.ComponentModel.DataAnnotations.Required]
  16. public string name { get; set; }
  17. public DateTime? birth { get; set; }
  18. public int? fatherId { get; set; }
  19. public int? motherId { get; set; }
  20. [System.ComponentModel.DataAnnotations.Schema.NotMapped]
  21. public string test{ get; set; }
  22. }
  23. public class DataSource
  24. {
  25. static string connectionString = Appsettings.json.GetStringByPath("App.Db.ConnectionString");
  26. public static SqlDbContext CreateDbContext()
  27. {
  28. var dbContext = new SqlDbContext();
  29. dbContext.UseSqlServer(connectionString);
  30. dbContext.BeginTransaction();
  31. var userSet = dbContext.DbSet<User>();
  32. dbContext.Execute(sql: "IF OBJECT_ID(N'User', N'U') IS NOT NULL \r\nDROP TABLE [User];");
  33. userSet.Create();
  34. var users = new List<User> {
  35. new User { name="u146", fatherId=4, motherId=6 },
  36. new User { name="u246", fatherId=4, motherId=6 },
  37. new User { name="u356", fatherId=5, motherId=6 },
  38. new User { name="u400" },
  39. new User { name="u500" },
  40. new User { name="u600" },
  41. };
  42. dbContext.AddRange(users);
  43. users.ForEach(user => { user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id); });
  44. dbContext.UpdateRange(users);
  45. return dbContext;
  46. }
  47. }
  48. }