using FreeSql; using System.Collections.Generic; using System.IO; namespace WIDESEA_Common.DBHelper { public class FreeDB { private static IFreeSql fsql; public static IFreeSql DB { get { if (fsql == null) { string coonStr = string.Empty; var configTxt = File.ReadAllText(Directory.GetCurrentDirectory() + "/appsettings.json"); var res = System.Text.RegularExpressions.Regex.Match(configTxt, "\"DbConnectionString\".*?:.*?\"(.*?)\""); if (res.Success) { coonStr = res.Groups[1].Value; } fsql = new FreeSql.FreeSqlBuilder() //.UseNoneCommandParameter(true) //.UseMonitorCommand(cmd => //{ // File.AppendAllText("sql.txt", cmd.CommandText + "\n\n"); //}) //.UseNoneCommandParameter(true) //.UseMonitorCommand(cmd => //{ // File.AppendAllText("sql.txt", cmd.CommandText + "\n\n"); //}) .UseConnectionString(FreeSql.DataType.SqlServer, coonStr) .Build(); } return fsql; } set { } } public IFreeSql DataBase = DB; //返回数据库结果 public int Remove(Entity entity) where Entity : class { return DB.Delete().Where(entity).ExecuteAffrows(); } //返回数据库结果 public int Add(Entity entity) where Entity : class { return DB.Insert(entity).ExecuteAffrows(); } //返回数据库结果 public int Add(List list) where Entity : class { return DB.Insert(list).ExecuteAffrows(); } //拓展 public ISelect Select() where Entity : class { return DB.Select(); } //拓展 public IUpdate Update(Entity entity) where Entity : class { return DB.Update().SetSource(entity); } //关闭 public static void Dispose() { fsql?.Dispose(); } } }