using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; using HHWMS.Web.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using WalkingTec.Mvvm.Core; using WalkingTec.Mvvm.Core.Extensions; namespace HHWMS.Web { public class DataContext : FrameworkContext { public DataContext(CS cs) : base(cs) { } public DataContext(string cs, DBTypeEnum dbtype) : base(cs, dbtype) { } public DataContext(string cs, DBTypeEnum dbtype, string version = null) : base(cs, dbtype, version) { } public DataContext(DbContextOptions options) : base(options) { } public DbSet FrameworkUsers { get; set; } public DbSet TaskHeaders { get; set; } public DbSet TaskDetails { get; set; } public DbSet TaskHeaderHistories { get; set; } public DbSet TaskDetailHistories { get; set; } public DbSet SysCounts { get; set; } public DbSet SysJobs { get; set; } public DbSet SysDictTypes { get; set; } public DbSet SysDictDatas { get; set; } public DbSet Locations { get; set; } public DbSet Containers { get; set; } public DbSet Inventories { get; set; } public DbSet InventoryTransactions { get; set; } public DbSet ReceiptHeaders { get; set; } public DbSet ReceiptDetails { get; set; } public DbSet ShipmentHeaders { get; set; } public DbSet ShipmentDetails { get; set; } public DbSet ReceiptHeaderHistories { get; set; } public DbSet ReceiptDetailHistories { get; set; } public DbSet ShipmentHeaderHistories { get; set; } public DbSet ShipmentDetailHistories { get; set; } public DbSet ReportDesigns { get; set; } public DbSet Materials { get; set; } public DbSet MaterialQrs { get; set; } public DbSet ReceiptHeader2s { get; set; } public DbSet ReceiptDetail2s { get; set; } public DbSet ShipmentHeader2s { get; set; } public DbSet ShipmentDetail2s { get; set; } public DbSet Suppliers { get; set; } public DbSet Material2s { get; set; } public override async Task DataInit(object allModules, bool IsSpa) { var state = await base.DataInit(allModules, IsSpa); bool emptydb = false; try { emptydb = Set().Count() == 0 && Set().Count() == 0; } catch { } if (state == true || emptydb == true) { //when state is true, means it's the first time EF create database, do data init here //当state是true的时候,表示这是第一次创建数据库,可以在这里进行数据初始化 var user = new FrameworkUser { ITCode = "admin", Password = Utils.GetMD5String("000000"), IsValid = true, Name = "Admin", TenantCode = TenantCode }; var userrole = new FrameworkUserRole { UserCode = user.ITCode, RoleCode = "001", TenantCode= TenantCode }; Set().Add(user); Set().Add(userrole); await SaveChangesAsync(); } return state; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // ef生成表 添加字段注释,只有本项目的表才添加,需要在DisplayAttribute,添加Description var ddd = modelBuilder.Model.GetEntityTypes(); ddd = ddd.Where(x => x.ClrType.FullName.Contains("HHWMS.Web")).ToList(); foreach (var item in ddd) { var props = item.ClrType.GetProperties(); var descriptionAttrtable = item.ClrType.GetCustomAttributes(typeof(DisplayAttribute), true); if (descriptionAttrtable.Length > 0) { modelBuilder.Entity(item.Name).HasComment(((DisplayAttribute)descriptionAttrtable[0]).Description ?? ""); } foreach (var prop in props) { var descriptionAttr = prop.GetCustomAttributes(typeof(DisplayAttribute), true); if (descriptionAttr.Length > 0) { modelBuilder.Entity(item.Name).Property(prop.Name).HasComment(((DisplayAttribute)descriptionAttr[0]).Description ?? ""); } } } } } public class DataContextFactory : IDesignTimeDbContextFactory { public DataContext CreateDbContext(string[] args) { return new DataContext("Server=(localdb)\\mssqllocaldb;Database=HHWMS;Trusted_Connection=True;MultipleActiveResultSets=true", DBTypeEnum.SqlServer); } } }