mirror of
https://gitee.com/iioter/iotgateway.git
synced 2024-12-02 03:38:01 +08:00
88 lines
2.8 KiB
C#
88 lines
2.8 KiB
C#
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
using IoTGateway.Model;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.EntityFrameworkCore.Design;
|
||
using WalkingTec.Mvvm.Core;
|
||
|
||
namespace IoTGateway.DataAccess
|
||
{
|
||
public class DataContext : FrameworkContext
|
||
{
|
||
public DbSet<FrameworkUser> FrameworkUsers { get; set; }
|
||
public DbSet<Device> Devices { get; set; }
|
||
public DbSet<DeviceConfig> DeviceConfigs { get; set; }
|
||
public DbSet<DeviceVariable> DeviceVariables { get; set; }
|
||
public DbSet<Driver> Drivers { get; set; }
|
||
public DbSet<SystemConfig> SystemConfig { get; set; }
|
||
public DbSet<RpcLog> RpcLogs { get; set; }
|
||
|
||
|
||
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 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"
|
||
};
|
||
|
||
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("Data Source = ../IoTGateway/iotgateway.db", DBTypeEnum.SQLite);
|
||
}
|
||
}
|
||
|
||
}
|