139 lines
5.7 KiB
C#
139 lines
5.7 KiB
C#
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<DataContext> options) : base(options) { }
|
||
public DbSet<FrameworkUser> FrameworkUsers { get; set; }
|
||
public DbSet<TaskHeader> TaskHeaders { get; set; }
|
||
public DbSet<TaskDetail> TaskDetails { get; set; }
|
||
public DbSet<TaskHeaderHistory> TaskHeaderHistories { get; set; }
|
||
public DbSet<TaskDetailHistory> TaskDetailHistories { get; set; }
|
||
public DbSet<SysCount> SysCounts { get; set; }
|
||
public DbSet<SysJob> SysJobs { get; set; }
|
||
public DbSet<SysDictType> SysDictTypes { get; set; }
|
||
public DbSet<SysDictData> SysDictDatas { get; set; }
|
||
public DbSet<Location> Locations { get; set; }
|
||
public DbSet<Container> Containers { get; set; }
|
||
public DbSet<Inventory> Inventories { get; set; }
|
||
public DbSet<InventoryTransaction> InventoryTransactions { get; set; }
|
||
public DbSet<ReceiptHeader> ReceiptHeaders { get; set; }
|
||
public DbSet<ReceiptDetail> ReceiptDetails { get; set; }
|
||
public DbSet<ShipmentHeader> ShipmentHeaders { get; set; }
|
||
public DbSet<ShipmentDetail> ShipmentDetails { get; set; }
|
||
public DbSet<ReceiptHeaderHistory> ReceiptHeaderHistories { get; set; }
|
||
public DbSet<ReceiptDetailHistory> ReceiptDetailHistories { get; set; }
|
||
public DbSet<ShipmentHeaderHistory> ShipmentHeaderHistories { get; set; }
|
||
public DbSet<ShipmentDetailHistory> ShipmentDetailHistories { get; set; }
|
||
public DbSet<ReportDesign> ReportDesigns { get; set; }
|
||
public DbSet<Material> Materials { get; set; }
|
||
public DbSet<MaterialQr> MaterialQrs { get; set; }
|
||
|
||
public DbSet<ReceiptHeader2> ReceiptHeader2s { get; set; }
|
||
public DbSet<ReceiptDetail2> ReceiptDetail2s { get; set; }
|
||
|
||
public DbSet<ShipmentHeader2> ShipmentHeader2s { get; set; }
|
||
public DbSet<ShipmentDetail2> ShipmentDetail2s { get; set; }
|
||
|
||
public DbSet<Supplier> Suppliers { get; set; }
|
||
public DbSet<Material2> Material2s { get; set; }
|
||
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",
|
||
TenantCode = TenantCode
|
||
};
|
||
|
||
var userrole = new FrameworkUserRole
|
||
{
|
||
UserCode = user.ITCode,
|
||
RoleCode = "001",
|
||
TenantCode= TenantCode
|
||
};
|
||
Set<FrameworkUser>().Add(user);
|
||
Set<FrameworkUserRole>().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<DataContext>
|
||
{
|
||
public DataContext CreateDbContext(string[] args)
|
||
{
|
||
return new DataContext("Server=(localdb)\\mssqllocaldb;Database=HHWMS;Trusted_Connection=True;MultipleActiveResultSets=true", DBTypeEnum.SqlServer);
|
||
}
|
||
}
|
||
}
|