using FreeSql;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Core.DBManager;
|
|
namespace WIDESEA_Core.FreeDB
|
{
|
public class FreeDB
|
{
|
private static IFreeSql DB;
|
|
public IFreeSql DataBase { get => DB; }
|
|
public FreeDB()
|
{
|
if (DB == null)
|
{
|
string coon = DBServerProvider.GetConnectionString();
|
DB = new FreeSql.FreeSqlBuilder()
|
.UseConnectionString(FreeSql.DataType.SqlServer, coon)
|
.Build();
|
}
|
}
|
|
/// <summary>
|
/// 插入数据
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="entity"></param>
|
/// <returns></returns>
|
public int Add<T>(T entity) where T : class
|
{
|
return DB.Insert<T>(entity).ExecuteAffrows();
|
}
|
|
/// <summary>
|
/// 批量插入
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="entityList"></param>
|
/// <returns></returns>
|
public int AddRange<T>(List<T> entityList) where T : class
|
{
|
return DB.Insert<T>(entityList).ExecuteAffrows();
|
}
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="entity"></param>
|
/// <returns></returns>
|
public int Remove<T>(T entity) where T : class
|
{
|
return DB.Delete<T>(entity).ExecuteAffrows();
|
}
|
|
/// <summary>
|
/// 更新(拓展方法)
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="entity"></param>
|
/// <returns></returns>
|
public IUpdate<T> Update<T>(T entity) where T : class
|
{
|
return DB.Update<T>(entity).SetSource(entity);
|
}
|
|
/// <summary>
|
/// 更新(拓展方法)
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <returns></returns>
|
public IUpdate<T> Update<T>() where T : class
|
{
|
return DB.Update<T>();
|
}
|
|
/// <summary>
|
/// 查询
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <returns></returns>
|
public ISelect<T> Select<T>() where T : class
|
{
|
return DB.Select<T>();
|
}
|
|
/// <summary>
|
/// 事务(完整事务)
|
/// </summary>
|
/// <param name="action"></param>
|
public void Transaction(Action action)
|
{
|
DB.Transaction(action);
|
}
|
|
/// <summary>
|
/// 事务(上下文事务)
|
/// </summary>
|
/// <param name="action"></param>
|
public void Context(Action<DbContext> action)
|
{
|
using (var db = DB.CreateDbContext())
|
{
|
action(db);
|
}
|
}
|
}
|
}
|