添加项目文件。
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
using Microsoft.Data.SqlClient;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BarCode.Web.Repositories.Configuration
|
||||
{
|
||||
/// <summary>
|
||||
/// db上下文扩展类
|
||||
/// </summary>
|
||||
public static class DbContextExtensions
|
||||
{
|
||||
public static IEnumerable<T> SqlQuery<T>(this DatabaseFacade facade, string sql, bool isAlias = false, params object[] parameters) where T : class, new()
|
||||
{
|
||||
var dt = SqlQuery(facade, sql, parameters);
|
||||
if (isAlias)
|
||||
return dt.ToEnumerableAlias<T>();
|
||||
return dt.ToEnumerable<T>();
|
||||
}
|
||||
|
||||
public static async Task<IEnumerable<T>> SqlQueryAsync<T>(this DatabaseFacade facade, string sql, bool isAlias = false, params object[] parameters) where T : class, new()
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
var dt = SqlQuery(facade, sql, parameters);
|
||||
if (isAlias)
|
||||
return dt.ToEnumerableAlias<T>();
|
||||
return dt.ToEnumerable<T>();
|
||||
});
|
||||
}
|
||||
|
||||
public static IEnumerable<T> ToEnumerable<T>(this DataTable dt) where T : class, new()
|
||||
{
|
||||
var propertyInfos = typeof(T).GetProperties();
|
||||
var ts = new T[dt.Rows.Count];
|
||||
var i = 0;
|
||||
foreach (DataRow row in dt.Rows)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
var t = new T();
|
||||
foreach (var p in propertyInfos)
|
||||
if (dt.Columns.IndexOf(p.Name) != -1 && row[p.Name] != DBNull.Value)
|
||||
p.SetValue(t, row[p.Name], null);
|
||||
|
||||
ts[i] = t;
|
||||
i++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
throw ex;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对象属性别名的映射关系
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> ToEnumerableAlias<T>(this DataTable dt) where T : class, new()
|
||||
{
|
||||
var propertyInfos = typeof(T).GetProperties();
|
||||
var ts = new T[dt.Rows.Count];
|
||||
var i = 0;
|
||||
foreach (DataRow row in dt.Rows)
|
||||
{
|
||||
var t = new T();
|
||||
foreach (var p in propertyInfos)
|
||||
{
|
||||
var attrs = p.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.Schema.ColumnAttribute), true);
|
||||
if (attrs.Length > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var name = ((System.ComponentModel.DataAnnotations.Schema.ColumnAttribute)attrs[0]).Name;
|
||||
if (dt.Columns.IndexOf(name) != -1 && row[name] != DBNull.Value)
|
||||
p.SetValue(t, row[name], null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ts[i] = t;
|
||||
i++;
|
||||
}
|
||||
|
||||
return ts;
|
||||
}
|
||||
|
||||
public static DataTable SqlQuery(this DatabaseFacade facade, string sql, params object[] parameters)
|
||||
{
|
||||
var cmd = CreateCommand(facade, sql, out var conn, parameters);
|
||||
var reader = cmd.ExecuteReader();
|
||||
var dt = new DataTable();
|
||||
dt.Load(reader);
|
||||
reader.Close();
|
||||
conn.Close();
|
||||
return dt;
|
||||
}
|
||||
|
||||
private static DbCommand CreateCommand(DatabaseFacade facade, string sql, out DbConnection dbConn, params object[] parameters)
|
||||
{
|
||||
var conn = facade.GetDbConnection();
|
||||
dbConn = conn;
|
||||
conn.Open();
|
||||
var cmd = conn.CreateCommand();
|
||||
if (facade.IsMySql())
|
||||
{
|
||||
cmd.CommandText = sql;
|
||||
CombineParamsMySql(ref cmd, parameters);
|
||||
}
|
||||
if (facade.IsSqlServer())
|
||||
{
|
||||
cmd.CommandText = sql;
|
||||
CombineParams(ref cmd, parameters);
|
||||
}
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
private static void CombineParams(ref DbCommand command, params object[] parameters)
|
||||
{
|
||||
if (parameters != null)
|
||||
foreach (SqlParameter parameter in parameters)
|
||||
{
|
||||
if (!parameter.ParameterName.Contains("@"))
|
||||
parameter.ParameterName = $"@{parameter.ParameterName}";
|
||||
command.Parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
private static void CombineParamsMySql(ref DbCommand command, params object[] parameters)
|
||||
{
|
||||
if (parameters != null)
|
||||
foreach (MySqlConnector.MySqlParameter parameter in parameters)
|
||||
{
|
||||
if (!parameter.ParameterName.Contains("@"))
|
||||
parameter.ParameterName = $"@{parameter.ParameterName}";
|
||||
command.Parameters.Add(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool ExecuteSqlCommand(this DatabaseFacade facade, string sql, params object[] parameters)
|
||||
{
|
||||
var cmd = CreateCommand(facade, sql, out var conn, parameters);
|
||||
try
|
||||
{
|
||||
cmd.ExecuteNonQuery();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
conn.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static IQueryable<T> SortBy<T>(this IQueryable<T> source, string sortExpression)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
throw new ArgumentNullException("source");
|
||||
}
|
||||
|
||||
string sortDirection = String.Empty;
|
||||
string propertyName = String.Empty;
|
||||
|
||||
sortExpression = sortExpression.Trim();
|
||||
int spaceIndex = sortExpression.Trim().IndexOf(" ");
|
||||
if (spaceIndex < 0)
|
||||
{
|
||||
propertyName = sortExpression;
|
||||
sortDirection = "ASC";
|
||||
}
|
||||
else
|
||||
{
|
||||
propertyName = sortExpression.Substring(0, spaceIndex);
|
||||
sortDirection = sortExpression.Substring(spaceIndex + 1).Trim();
|
||||
}
|
||||
|
||||
if (String.IsNullOrEmpty(propertyName))
|
||||
{
|
||||
return source;
|
||||
}
|
||||
|
||||
ParameterExpression parameter = Expression.Parameter(source.ElementType, String.Empty);
|
||||
MemberExpression property = Expression.Property(parameter, propertyName);
|
||||
LambdaExpression lambda = Expression.Lambda(property, parameter);
|
||||
|
||||
string methodName = (sortDirection == "ASC") ? "OrderBy" : "OrderByDescending";
|
||||
|
||||
Expression methodCallExpression = Expression.Call(typeof(Queryable), methodName,
|
||||
new Type[] { source.ElementType, property.Type },
|
||||
source.Expression, Expression.Quote(lambda));
|
||||
|
||||
return source.Provider.CreateQuery<T>(methodCallExpression);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace BarCode.Web.Repositories.Configuration.EFLog
|
||||
{
|
||||
public class EFCoreLogger : ILogger
|
||||
{
|
||||
private readonly string categoryName;
|
||||
|
||||
public EFCoreLogger(string categoryName) => this.categoryName = categoryName;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
|
||||
{
|
||||
if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command" && logLevel == LogLevel.Information)
|
||||
{
|
||||
var logContent = formatter(state, exception);
|
||||
var str = logContent.ToLower();
|
||||
if ((str.Contains("update") || str.Contains("delete") || str.Contains("insert")) &&
|
||||
(str.Contains("t_sub_ppbom") || str.Contains("t_sub_ppbomentry") || str.Contains("t_sub_ppbomentry_l")
|
||||
|| str.Contains("t_prd_ppbom") || str.Contains("t_prd_ppbomentry") || str.Contains("t_prd_ppbomentry_l")
|
||||
|| str.Contains("t_eng_bom") || str.Contains("t_eng_bomchild")))
|
||||
{
|
||||
if (!Directory.Exists("D:/Logs"))
|
||||
Directory.CreateDirectory("D:/Logs");
|
||||
|
||||
logContent = "\r\n\r\n" + DateTime.Now + "--" + logContent;
|
||||
//没有文件会自动创建,有就追加
|
||||
System.IO.File.AppendAllText("D:/Logs/增删改记录.txt", logContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
public IDisposable BeginScope<TState>(TState state) => null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BarCode.Web.Repositories.Configuration.EFLog
|
||||
{
|
||||
public class EFCoreLoggerProvider : ILoggerProvider
|
||||
{
|
||||
public ILogger CreateLogger(string categoryName) => new EFCoreLogger(categoryName);
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Debug;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using BarCode.Web.Domain.Entitys;
|
||||
using BarCode.Web.Repositories.Configuration.EFLog;
|
||||
|
||||
namespace BarCode.Web.Repositories.Configuration
|
||||
{
|
||||
/// <summary>
|
||||
/// db上下文
|
||||
/// </summary>
|
||||
public class RepositoryDbContext : DbContext
|
||||
{
|
||||
[Obsolete]
|
||||
public readonly LoggerFactory LoggerFactory = new LoggerFactory(new[] { new DebugLoggerProvider() });
|
||||
public RepositoryDbContext(DbContextOptions<RepositoryDbContext> options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
LoggerFactory.AddProvider(new EFCoreLoggerProvider());
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
optionsBuilder.UseLoggerFactory(LoggerFactory).EnableSensitiveDataLogging();
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
builder.Entity<FileDownManager>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_file_down_manager");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
|
||||
|
||||
//箱唛表
|
||||
builder.Entity<BoxMark>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_box_mark");
|
||||
ent.HasKey(x => x.Id);
|
||||
|
||||
ent.HasMany(p => p.BillNos)
|
||||
.WithOne()
|
||||
.HasForeignKey(p => p.Fid)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
|
||||
//箱唛表-编号表
|
||||
builder.Entity<BoxMarkBillNo>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_box_mark_billno");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
//物料
|
||||
builder.Entity<Materials>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_materials");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
|
||||
//序列号生成记录
|
||||
builder.Entity<SerialNumberGenerateRecord>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_serialnumbergeneraterecord");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
|
||||
//序列号
|
||||
builder.Entity<SerialNumbers>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_serialnumbers");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
|
||||
//防伪码生成记录
|
||||
builder.Entity<SecurityNumberGenerateRecord>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_securitynumbergeneraterecord");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
|
||||
//防伪码
|
||||
builder.Entity<SecurityNumbers>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_securitynumbers");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
|
||||
#region 箱信息
|
||||
builder.Entity<Box>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_box");
|
||||
ent.HasKey(x => x.Id);
|
||||
|
||||
ent.HasMany(p => p.Details)
|
||||
.WithOne()
|
||||
.HasForeignKey(p => p.Fid)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
});
|
||||
builder.Entity<BoxDetails>(ent =>
|
||||
{
|
||||
ent.ToTable("t_barcode_box_details");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
#endregion
|
||||
|
||||
//生成数据管理
|
||||
builder.Entity<CenerateData>(ent =>
|
||||
{
|
||||
ent.ToTable("t_wms_cenerate_date");
|
||||
ent.HasKey(x => x.Id);
|
||||
});
|
||||
|
||||
base.OnModelCreating(builder);
|
||||
}
|
||||
public DbSet<Materials> Materials { get; set; }
|
||||
public DbSet<BoxMarkBillNo> BoxMarkBillNo { get; set; }
|
||||
public DbSet<BoxMark> BoxMark { get; set; }
|
||||
public DbSet<FileDownManager> FileDownManager { get; set; }
|
||||
public DbSet<SerialNumberGenerateRecord> SerialNumberGenerateRecord { get; set; }
|
||||
public DbSet<SerialNumbers> SerialNumbers { get; set; }
|
||||
public DbSet<Box> Box { get; set; }
|
||||
public DbSet<BoxDetails> BoxDetails { get; set; }
|
||||
public DbSet<SecurityNumberGenerateRecord> SecurityNumberGenerateRecord { get; set; }
|
||||
public DbSet<SecurityNumbers> SecurityNumbers { get; set; }
|
||||
public DbSet<CenerateData> CenerateData { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user