using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using WalkingTec.Mvvm.Core; using wtmProject.Model; namespace wtmProject.DataAccess { public class DataContext : FrameworkContext { public DbSet WTM_BILLING_LIST_RECEIVED_FILE { get; set; } public DbSet WTM_BILLING_LIST_BILLING_FILE { get; set; } public DbSet WTM_BILLING_LIST { get; set; } public DbSet WTM_CONTRACT_FILE { get; set; } public DbSet WTM_CONTRACT { get; set; } public DbSet WTM_BUSINESS { get; set; } public DbSet WTM_VISIT_PLAN_USER { get; set; } public DbSet WTM_VISIT_PLAN { get; set; } public DbSet WTM_CUSTOM_PERSON { get; set; } public DbSet WTM_CUSTOM { get; set; } public DbSet WTM_USER_AREA { get; set; } public DbSet WTM_USER_PROJECT { get; set; } public DbSet WTM_PROJECT { get; set; } public DbSet WTM_AREA { get; set; } public DbSet FrameworkOutRecords { get; set; } public DbSet FrameworkProjects { get; set; } public DbSet FrameworkCustoms { get; set; } public DbSet FrameworkUserPosts { get; set; } public DbSet FrameworkPosts { get; set; } public DbSet FrameworkUsers { get; set; } public DataContext(CS cs) : base(cs) { } public DataContext(string cs, DBTypeEnum dbtype) : base(cs, dbtype) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasOne(t => t.Billing).WithMany(t => t.ReceivedFiles).HasForeignKey(t => t.BillingId).OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity().HasOne(t => t.Billing).WithMany(t => t.BillingFiles).HasForeignKey(t => t.BillingId).OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity().HasOne(t => t.Contract).WithMany(t => t.Files).HasForeignKey(t => t.ContractId).OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity().HasOne(t => t.VisitPlan).WithMany(t => t.VisitUser).HasForeignKey(t => t.VisitPlanId).OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity().HasOne(t => t.Custom).WithMany(t => t.CustomPerson).HasForeignKey(t => t.CustomId).OnDelete(DeleteBehavior.Restrict); modelBuilder.Entity().HasOne(t => t.Group).WithMany().HasForeignKey(t => t.GroupId).OnDelete(DeleteBehavior.Restrict); base.OnModelCreating(modelBuilder); } public DataContext(string cs, DBTypeEnum dbtype, string version = null) : base(cs, dbtype, version) { } public DataContext(DbContextOptions options) : base(options) { } 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" }; var userrole = new FrameworkUserRole { UserCode = user.ITCode, RoleCode = "001" }; var adminmenus = Set().Where(x => x.Url != null && x.Url.StartsWith("/api") == false).ToList(); foreach (var item in adminmenus) { item.Url = "/_admin" + item.Url; } Set().Add(user); Set().Add(userrole); await SaveChangesAsync(); } return state; } } /// /// DesignTimeFactory for EF Migration, use your full connection string, /// EF will find this class and use the connection defined here to run Add-Migration and Update-Database /// public class DataContextFactory : IDesignTimeDbContextFactory { public DataContext CreateDbContext(string[] args) { return new DataContext("Server=47.122.43.22;Database=wtmProject_db;uid=sa;pwd=mhsafe!2021;", DBTypeEnum.SqlServer);//Server=(localdb)\\mssqllocaldb;Database=wtmProject_db;Trusted_Connection=True; } } }