|
@@ -1,6 +1,7 @@
|
|
|
using SQLite.CodeFirst;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.ComponentModel.DataAnnotations.Schema;
|
|
|
using System.Data.Common;
|
|
|
using System.Data.Entity;
|
|
|
using System.Data.Entity.Core.Common;
|
|
@@ -12,13 +13,16 @@ using System.Data.SQLite;
|
|
|
using System.Data.SQLite.EF6;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
+using System.Reflection;
|
|
|
using System.Text.RegularExpressions;
|
|
|
+using System.Threading.Tasks;
|
|
|
using XdCxRhDW.Entity;
|
|
|
|
|
|
namespace XdCxRhDW.Repostory
|
|
|
{
|
|
|
public class RHDWLogContext : DbContext
|
|
|
{
|
|
|
+
|
|
|
public RHDWLogContext() : base("LogDbCon") //配置使用的连接名
|
|
|
{
|
|
|
}
|
|
@@ -41,18 +45,131 @@ namespace XdCxRhDW.Repostory
|
|
|
/// </summary>
|
|
|
public class RHDWContext : DbContext
|
|
|
{
|
|
|
+ private class DbTableColumnInfo
|
|
|
+ {
|
|
|
+ public string name { get; set; }
|
|
|
+
|
|
|
+ public string type { get; set; }
|
|
|
+
|
|
|
+ public int notnull { get; set; }
|
|
|
+
|
|
|
+ public int pk { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class DbTableForeignKeyInfo
|
|
|
+ {
|
|
|
+ public string from { get; set; }
|
|
|
+ }
|
|
|
public RHDWContext() : base("DbCon") //配置使用的连接名
|
|
|
{
|
|
|
}
|
|
|
- //protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items)
|
|
|
- //{
|
|
|
- // var tables = this.Database.SqlQuery<string>("select name from sqlite_master where type='table' and name not like 'sqlite%'").ToList();
|
|
|
- // return base.ValidateEntity(entityEntry, items);
|
|
|
- //}
|
|
|
- //protected override bool ShouldValidateEntity(DbEntityEntry entityEntry)
|
|
|
- //{
|
|
|
- // return base.ShouldValidateEntity(entityEntry);
|
|
|
- //}
|
|
|
+
|
|
|
+ public Task<List<T>> SqlQueryAsync<T>(string sql)
|
|
|
+ {
|
|
|
+ return this.Database.SqlQuery<T>(sql).ToListAsync();
|
|
|
+ }
|
|
|
+ public Task<T> SqlQueryOneAsync<T>(string sql)
|
|
|
+ {
|
|
|
+ return this.Database.SqlQuery<T>(sql).FirstOrDefaultAsync();
|
|
|
+
|
|
|
+ }
|
|
|
+ //检查数据库表是否缺失
|
|
|
+ public string CheckTableExist()
|
|
|
+ {
|
|
|
+ var tables = this.Database.SqlQuery<string>("select name from sqlite_master where type='table' and name not like 'sqlite%'").ToList();
|
|
|
+ var props = this.GetType().GetProperties();
|
|
|
+ List<string> list = new List<string>();
|
|
|
+ foreach (var prop in props)
|
|
|
+ {
|
|
|
+ bool isDbSet = prop.PropertyType.IsGenericType && typeof(DbSet<>) == prop.PropertyType.GetGenericTypeDefinition();
|
|
|
+ if (isDbSet)
|
|
|
+ {
|
|
|
+ var entityType = prop.PropertyType.GenericTypeArguments[0];
|
|
|
+ var name = entityType.GetCustomAttribute<TableAttribute>()?.Name;
|
|
|
+ if (name == null)
|
|
|
+ {
|
|
|
+ name = entityType.Name;
|
|
|
+ }
|
|
|
+ list.Add(name);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (var item in list)
|
|
|
+ {
|
|
|
+ if (!tables.Contains(item))
|
|
|
+ {
|
|
|
+ return item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ public string CheckTableField()
|
|
|
+ {
|
|
|
+ var tables = this.Database.SqlQuery<string>("select name from sqlite_master where type='table' and name not like 'sqlite%'").ToList();
|
|
|
+ var props = this.GetType().GetProperties();
|
|
|
+ List<Type> entityTypes = new List<Type>();
|
|
|
+ foreach (var prop in props)
|
|
|
+ {
|
|
|
+ bool isDbSet = prop.PropertyType.IsGenericType && typeof(DbSet<>) == prop.PropertyType.GetGenericTypeDefinition();
|
|
|
+ if (isDbSet)
|
|
|
+ {
|
|
|
+ var entityType = prop.PropertyType.GenericTypeArguments[0];
|
|
|
+ entityTypes.Add(entityType);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (var table in tables)
|
|
|
+ {
|
|
|
+ var res = this.Database.SqlQuery<DbTableColumnInfo>($"PRAGMA table_info([{table}])").ToList();
|
|
|
+ var entityType = entityTypes.First(p => p.Name == table);
|
|
|
+ var entityProps = entityType.GetProperties().Where(p =>
|
|
|
+ p.CanRead
|
|
|
+ && p.CanWrite
|
|
|
+ && !p.GetMethod.IsVirtual
|
|
|
+ && p.GetCustomAttribute<NotMappedAttribute>() == null);
|
|
|
+ foreach (var prop in entityProps)
|
|
|
+ {
|
|
|
+ var find = res.Find(p => p.name == prop.Name);
|
|
|
+ if (find == null)
|
|
|
+ {
|
|
|
+ string typeStr = "";
|
|
|
+ var type = prop.PropertyType;
|
|
|
+ if (prop.PropertyType.IsGenericType)
|
|
|
+ {
|
|
|
+ type = prop.PropertyType.GenericTypeArguments[0];
|
|
|
+ }
|
|
|
+ if (type == typeof(string))
|
|
|
+ {
|
|
|
+ typeStr = "nvarchar";
|
|
|
+ }
|
|
|
+ else if (type == typeof(int) || type == typeof(long))
|
|
|
+ {
|
|
|
+ typeStr = "int";
|
|
|
+ }
|
|
|
+ else if (type == typeof(double) || type == typeof(float))
|
|
|
+ {
|
|
|
+ typeStr = "float";
|
|
|
+ }
|
|
|
+ else if (type == typeof(DateTime))
|
|
|
+ {
|
|
|
+ typeStr = "datetime";
|
|
|
+ }
|
|
|
+ return $"Database.db数据库表{table}缺少{prop.Name}字段,类型={typeStr}";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ if (prop.PropertyType != typeof(string) && find.pk == 0 && !prop.PropertyType.IsGenericType && find.notnull == 0)
|
|
|
+ {
|
|
|
+ var foreignInfo = this.Database.SqlQuery<DbTableForeignKeyInfo>($"PRAGMA foreign_key_list({table})").ToList();
|
|
|
+ if (!foreignInfo.Any(p => p.from == prop.Name))
|
|
|
+ {
|
|
|
+ return $"Database.db数据库表{table}中{prop.Name}字段不允许为空";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
protected override void OnModelCreating(DbModelBuilder modelBuilder)
|
|
|
{
|
|
|
this.Database.Log = msg =>
|