C# 資料操作系列 - 15 SqlSugar 增刪改查詳解
阿新 • • 發佈:2020-05-25
# 0. 前言
繼上一篇,以及上上篇,我們對SqlSugar有了一個大概的認識,但是這並不完美,因為那些都是理論知識,無法描述我們工程開發中實際情況。而這一篇,將帶領小夥伴們一起試著寫一個能在工程中使用的模板類。
# 1. 建立一個Client
SqlSugar在操作的時候需要一個Client,用來管理資料庫連線,並操作資料庫。所以我們寫一個DbContext用來建立Client:
```c#
public class DefaultContext
{
public SqlSugarClient Client { get; }
public DefaultContext(string connectionString, DbType dbType)
{
Client = new SqlSugarClient(new ConnectionConfig
{
ConnectionString = connectionString,//"Data Source=./demo.db",
DbType = dbType,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute
});
Client.CodeFirst.InitTables(typeof(Dept), typeof(Person), typeof(Employee));
Client.Aop.OnLogExecuting = (sql, paramters) =>
{
Console.WriteLine(sql);
};
}
public Simp