mh_custom/wtmProject.DataAccess/DataContext.cs

120 lines
5.8 KiB
C#
Raw Normal View History

2026-01-06 10:55:05 +08:00
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_YEAR> WTM_BILLING_LIST_YEAR { get; set; }
public DbSet<WTM_CONTRACT_IMPORT_TEMP> WTM_CONTRACT_IMPORT_TEMP { get; set; }
public DbSet<WTM_BILLING_LIST_RECEIVED_FILE> WTM_BILLING_LIST_RECEIVED_FILE { get; set; }
public DbSet<WTM_BILLING_LIST_BILLING_FILE> WTM_BILLING_LIST_BILLING_FILE { get; set; }
public DbSet<WTM_BILLING_LIST> WTM_BILLING_LIST { get; set; }
public DbSet<WTM_CONTRACT_FILE> WTM_CONTRACT_FILE { get; set; }
public DbSet<WTM_CONTRACT> WTM_CONTRACT { get; set; }
public DbSet<WTM_BUSINESS> WTM_BUSINESS { get; set; }
public DbSet<WTM_VISIT_PLAN_USER> WTM_VISIT_PLAN_USER { get; set; }
public DbSet<WTM_VISIT_PLAN> WTM_VISIT_PLAN { get; set; }
public DbSet<WTM_CUSTOM_PERSON> WTM_CUSTOM_PERSON { get; set; }
public DbSet<WTM_CUSTOM> WTM_CUSTOM { get; set; }
public DbSet<WTM_USER_AREA> WTM_USER_AREA { get; set; }
public DbSet<WTM_USER_PROJECT> WTM_USER_PROJECT { get; set; }
public DbSet<WTM_PROJECT> WTM_PROJECT { get; set; }
public DbSet<WTM_AREA> WTM_AREA { get; set; }
public DbSet<FrameworkOutRecord> FrameworkOutRecords { get; set; }
public DbSet<FrameworkProject> FrameworkProjects { get; set; }
public DbSet<FrameworkCustom> FrameworkCustoms { get; set; }
public DbSet<FrameworkUserPost> FrameworkUserPosts { get; set; }
public DbSet<FrameworkPost> FrameworkPosts { get; set; }
public DbSet<FrameworkUser> 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<WTM_BILLING_LIST_RECEIVED_FILE>().HasOne(t => t.Billing).WithMany(t => t.ReceivedFiles).HasForeignKey(t => t.BillingId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<WTM_BILLING_LIST_BILLING_FILE>().HasOne(t => t.Billing).WithMany(t => t.BillingFiles).HasForeignKey(t => t.BillingId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<WTM_CONTRACT_FILE>().HasOne(t => t.Contract).WithMany(t => t.Files).HasForeignKey(t => t.ContractId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<WTM_VISIT_PLAN_FILE>().HasOne(t => t.VisitPlan).WithMany(t => t.Files).HasForeignKey(t => t.VisitPlanId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<WTM_VISIT_PLAN_USER>().HasOne(t => t.VisitPlan).WithMany(t => t.VisitUser).HasForeignKey(t => t.VisitPlanId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<WTM_CUSTOM_PERSON>().HasOne(t => t.Custom).WithMany(t => t.CustomPerson).HasForeignKey(t => t.CustomId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<FrameworkUser>().HasOne(t => t.Group).WithMany().HasForeignKey(t => t.GroupId).OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<WTM_CUSTOM>().Ignore(t => t.CustomName);
base.OnModelCreating(modelBuilder);
}
public DataContext(string cs, DBTypeEnum dbtype, string version = null)
: base(cs, dbtype, version)
{
}
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
public override async Task<bool> DataInit(object allModules, bool IsSpa)
{
var state = await base.DataInit(allModules, IsSpa);
bool emptydb = false;
try
{
emptydb = Set<FrameworkUser>().Count() == 0 && Set<FrameworkUserRole>().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<FrameworkMenu>().Where(x => x.Url != null && x.Url.StartsWith("/api") == false).ToList();
foreach (var item in adminmenus)
{
item.Url = "/_admin" + item.Url;
}
Set<FrameworkUser>().Add(user);
Set<FrameworkUserRole>().Add(userrole);
await SaveChangesAsync();
}
return state;
}
}
/// <summary>
/// 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
/// </summary>
public class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
public DataContext CreateDbContext(string[] args)
{
return new DataContext("Server=121.41.2.71;Database=wtmProject_db;uid=sa;pwd=mhsafe!2021;", DBTypeEnum.SqlServer);//Server=(localdb)\\mssqllocaldb;Database=wtmProject_db;Trusted_Connection=True;
}
}
}