电子药箱通讯服务端
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.

ApplicationDbContext.cs 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Microsoft.EntityFrameworkCore;
  2. using Microsoft.EntityFrameworkCore.ChangeTracking;
  3. using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
  4. using Rdh.ElectronicMedicineKit.Models;
  5. using System;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. namespace Rdh.ElectronicMedicineKit.EntityFrameworkCore
  9. {
  10. public class ApplicationDbContext : DbContext
  11. {
  12. public DbSet<DrugInfo> DrugInfos { get; set; }
  13. public DbSet<DrugBatch> DrugBatches { get; set; }
  14. public DbSet<Drug> Drugs { get; set; }
  15. public DbSet<Patient> Patients { get; set; }
  16. public DbSet<User> Users { get; set; }
  17. public DbSet<Operation> Operations { get; set; }
  18. public DbSet<OperationRoom> OperationRooms { get; set; }
  19. public DbSet<OperationDrug> OperationDrugs { get; set; }
  20. public DbSet<MedicineKit> MedicineKits { get; set; }
  21. public DbSet<MedicineKitLocation> MedicineKitLocations { get; set; }
  22. public DbSet<MedicineKitConfig> MedicineKitConfigs { get; set; }
  23. public DbSet<MedicineKitStock> MedicineKitStocks { get; set; }
  24. public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
  25. {
  26. ChangeTracker.StateChanged += UpdateTimestamps;
  27. ChangeTracker.Tracked += UpdateTimestamps;
  28. }
  29. protected override void OnModelCreating(ModelBuilder builder)
  30. {
  31. base.OnModelCreating(builder);
  32. foreach (var entityType in builder.Model.GetEntityTypes().Where(e => typeof(RootEntity).IsAssignableFrom(e.ClrType)))
  33. {
  34. // 添加全局 软删除 过滤器
  35. var isDeletedProperty = entityType.FindProperty(nameof(RootEntity.IsDeleted));
  36. if (isDeletedProperty != null)
  37. {
  38. var parameter = Expression.Parameter(entityType.ClrType, "p");
  39. var filterIsDeleted = Expression.Equal(Expression.Property(parameter, isDeletedProperty.PropertyInfo), Expression.Constant(false, typeof(bool)));
  40. entityType.SetQueryFilter(Expression.Lambda(filterIsDeleted, parameter));
  41. }
  42. isDeletedProperty.SetDefaultValue(false);
  43. foreach (var prop in entityType.GetProperties().Where(e => typeof(Guid).IsAssignableFrom(e.ClrType) || typeof(Guid?).IsAssignableFrom(e.ClrType)))
  44. {
  45. prop.SetValueConverter(new GuidToStringConverter());
  46. }
  47. }
  48. builder.Entity<User>(model => model.HasData(new User[]
  49. {
  50. new User { Name = "李浩" ,Code="a3ce8445"},
  51. new User { Name = "罗岩",Code="bb332e5a" },
  52. new User { Name = "刘强与" ,Code="bb332e52"},
  53. new User { Name = "文芳",Code="bb332e53" },
  54. new User { Name = "何颖" ,Code="bb332e54"},
  55. new User { Name = "张向丽",Code="bb332e55" },
  56. new User { Name = "汪玲" ,Code="bb332e56"},
  57. new User { Name = "李长风",Code="bb332e57" },
  58. new User { Name = "吴爱国" ,Code="bb332e58"},
  59. new User { Name = "白丽",Code="bb332e59" },
  60. new User { Name = "刘思雨" ,Code="bb332f54"},
  61. new User { Name = "张宇",Code="bb332e51" }
  62. }));
  63. builder.Entity<OperationRoom>(model => model.HasData(new OperationRoom[]
  64. {
  65. new OperationRoom{ Name = "手术间1", BaseStationCode="00"},
  66. new OperationRoom{Name="手术间2",BaseStationCode = "01"},
  67. new OperationRoom{Name="手术间3",BaseStationCode="02"}
  68. }));
  69. builder.Entity<Patient>(model => model.HasData(new Patient[]
  70. {
  71. new Patient { Name="陶竹生",PatientNo="10232853",Gender = Genders.男,BirthDay = DateTime.Parse("1995-12-01")} ,
  72. new Patient { Name="郦夏芳",PatientNo="10229717",Gender = Genders.女,BirthDay = DateTime.Parse("1987-12-01")} ,
  73. new Patient { Name="雷胜蓝",PatientNo="10251888",Gender = Genders.女,BirthDay = DateTime.Parse("1977-12-01")} ,
  74. new Patient { Name="张和初",PatientNo="10222577",Gender = Genders.男,BirthDay = DateTime.Parse("1996-12-01")} ,
  75. new Patient { Name="王美华",PatientNo="10221218",Gender = Genders.女,BirthDay = DateTime.Parse("1985-12-01")} ,
  76. new Patient { Name="谷乐平",PatientNo="10228969",Gender = Genders.男,BirthDay = DateTime.Parse("1975-12-01")} ,
  77. new Patient { Name="尤梅",PatientNo="10229521",Gender = Genders.女,BirthDay = DateTime.Parse("1999-12-01")} ,
  78. new Patient { Name="骆元方",PatientNo="10228510",Gender = Genders.男,BirthDay = DateTime.Parse("1965-12-01")}
  79. }));
  80. builder.Entity<MedicineKit>(model => model.HasData(new MedicineKit[]
  81. {
  82. new MedicineKit{ Code="000"}
  83. }));
  84. }
  85. private static void UpdateTimestamps(object sender, EntityEntryEventArgs e)
  86. {
  87. if (e.Entry.Entity is RootEntity entity)
  88. {
  89. switch (e.Entry.State)
  90. {
  91. case EntityState.Deleted:
  92. entity.IsDeleted = true;
  93. entity.DeletionTime = DateTime.Now;
  94. e.Entry.State = EntityState.Modified;
  95. break;
  96. case EntityState.Modified:
  97. entity.LastModificationTime = DateTime.Now;
  98. break;
  99. case EntityState.Added:
  100. entity.CreationTime = DateTime.Now;
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. }