mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-05 13:17:57 +08:00
81 lines
2.0 KiB
Plaintext
81 lines
2.0 KiB
Plaintext
using @Model.ClassNamespace;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
public class DbContext<T> where T : class, new()
|
|
{
|
|
public DbContext()
|
|
{
|
|
Db = new SqlSugarClient(new ConnectionConfig()
|
|
{
|
|
ConnectionString = "@Model.ConnectionString",
|
|
DbType = DbType.@Model.DbType,
|
|
InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
|
|
IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
|
|
|
|
});
|
|
//调式代码 用来打印SQL
|
|
Db.Aop.OnLogExecuting = (sql, pars) =>
|
|
{
|
|
Console.WriteLine(sql + "\r\n" +
|
|
Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
Console.WriteLine();
|
|
};
|
|
|
|
}
|
|
//注意:不能写成静态的
|
|
public SqlSugarClient Db;//用来处理事务多表查询和复杂的操作
|
|
public SimpleClient<T> CurrentDb { get { return new SimpleClient<T>(Db); } }
|
|
|
|
@foreach(var item in @Model.Tables)
|
|
{
|
|
@: public SimpleClient<@item> @(item)Db { get { return new SimpleClient<@item>(Db); } }//用来处理Student表的常用操作
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取所有
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public virtual List<T> GetList()
|
|
{
|
|
return CurrentDb.GetList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据主键删除
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public virtual bool Delete(dynamic id)
|
|
{
|
|
return CurrentDb.Delete(id);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 更新
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public virtual bool Update(T obj)
|
|
{
|
|
return CurrentDb.Update(obj);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 插入
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public virtual bool Insert(T obj)
|
|
{
|
|
return CurrentDb.Insert(obj);
|
|
}
|
|
|
|
//自已扩展更多方法
|
|
}
|
|
|
|
|