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 entity) where Entity : class
|
{
|
return DB.Delete<Entity>().Where(entity).ExecuteAffrows();
|
}
|
|
//返回数据库结果
|
public int Add<Entity>(Entity entity) where Entity : class
|
{
|
return DB.Insert(entity).ExecuteAffrows();
|
}
|
|
//返回数据库结果
|
public int Add<Entity>(List<Entity> list) where Entity : class
|
{
|
return DB.Insert(list).ExecuteAffrows();
|
}
|
|
//拓展
|
public ISelect<Entity> Select<Entity>() where Entity : class
|
{
|
return DB.Select<Entity>();
|
}
|
|
//拓展
|
public IUpdate<Entity> Update<Entity>(Entity entity) where Entity : class
|
{
|
return DB.Update<Entity>().SetSource(entity);
|
}
|
|
//关闭
|
public static void Dispose()
|
{
|
fsql?.Dispose();
|
}
|
}
|
}
|