123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Vit.Core.Util.ConfigurationManager;
- using Vitorm.Sql;
- namespace Vitorm.MsTest
- {
- [System.ComponentModel.DataAnnotations.Schema.Table("User")]
- public class User
- {
- [System.ComponentModel.DataAnnotations.Key]
- [System.ComponentModel.DataAnnotations.Schema.Column("userId")]
- [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
- public int id { get; set; }
- [System.ComponentModel.DataAnnotations.Schema.Column("userName")]
- public string name { get; set; }
- [System.ComponentModel.DataAnnotations.Schema.Column("userBirth")]
- public DateTime? birth { get; set; }
- [System.ComponentModel.DataAnnotations.Schema.Column("userFatherId")]
- public int? fatherId { get; set; }
- [System.ComponentModel.DataAnnotations.Schema.Column("userMotherId")]
- public int? motherId { get; set; }
- [System.ComponentModel.DataAnnotations.Schema.Column("userClassId")]
- public int? classId { get; set; }
- [System.ComponentModel.DataAnnotations.Schema.NotMapped]
- public string test { get; set; }
- public static User NewUser(int id, bool forAdd = false) => new User { id = forAdd ? 0 : id, name = "testUser" + id };
- public static List<User> NewUsers(int startId, int count = 1, bool forAdd = false)
- {
- return Enumerable.Range(startId, count).Select(id => NewUser(id, forAdd)).ToList();
- }
- }
- [System.ComponentModel.DataAnnotations.Schema.Table("UserClass")]
- public class UserClass
- {
- [System.ComponentModel.DataAnnotations.Key]
- [System.ComponentModel.DataAnnotations.Schema.Column("classId")]
- [System.ComponentModel.DataAnnotations.Schema.DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
- public int id { get; set; }
- [System.ComponentModel.DataAnnotations.Schema.Column("className")]
- public string name { get; set; }
- public static List<UserClass> NewClasses(int startId, int count = 1)
- {
- return Enumerable.Range(startId, count).Select(id => new UserClass { id = 0, name = "class" + id }).ToList();
- }
- }
- public class DataSource
- {
- public static void WaitForUpdate() { }
- static string connectionString = Appsettings.json.GetStringByPath("Vitorm.MySql.connectionString");
- public static SqlDbContext CreateDbContextForWriting() => CreateDbContext();
- public static SqlDbContext CreateDbContext()
- {
- var dbContext = new SqlDbContext();
- dbContext.UseMySql(connectionString);
- dbContext.BeginTransaction();
- #region #1 init User
- {
- dbContext.Drop<User>();
- dbContext.Create<User>();
- var users = new List<User> {
- new User { name="u146", fatherId=4, motherId=6 },
- new User { name="u246", fatherId=4, motherId=6 },
- new User { name="u356", fatherId=5, motherId=6 },
- new User { name="u400" },
- new User { name="u500" },
- new User { name="u600" },
- };
- dbContext.AddRange(users);
- users.ForEach(user =>
- {
- user.birth = DateTime.Parse("2021-01-01 00:00:00").AddHours(user.id);
- user.classId = user.id % 2 + 1;
- });
- dbContext.UpdateRange(users);
- }
- #endregion
- #region #2 init Class
- {
- dbContext.Drop<UserClass>();
- dbContext.Create<UserClass>();
- dbContext.AddRange(UserClass.NewClasses(1, 6));
- }
- #endregion
- return dbContext;
- }
- }
- }
|