//SqlSugarHelper.DemoDbContext.GenerateModels();
var list = SqlSugarHelper.DemoDbContext.Query<ORMClsLib.dbo.DemoEntity>();
var item = new ORMClsLib.dbo.DemoEntity()
{
operatorName = "test",
};
SqlSugarHelper.DemoDbContext.InsertOrUpdate(item);
SqlSugarHelper.DemoDbContext.Delete<ORMClsLib.dbo.DemoEntity>(0);
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
namespace ORMClsLib
{
public class SqlSugarHelper
{
/// <summary>
/// 参数
/// </summary>
private static readonly DefaultParams _defaultParams = DefaultParams.Gethandler();
//用单例模式
private static SqlSugarScope ServerDb = new SqlSugarScope(new ConnectionConfig()
{ //连接符字串
ConnectionString = string.Format("server={0};database={1};uid=sa;pwd={2}", _defaultParams.IpSource, _defaultParams.DatabaseName, _defaultParams.Password),
DbType = DbType.SqlServer,//数据库类型
IsAutoCloseConnection = true //不设成true要手动close
},
db =>
{
//(A)全局生效配置点
//调试SQL事件,可以删掉
db.Aop.OnLogExecuting = (sql, pars) =>
{
//获取原生SQL
Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
};
});
public static DbContext DemoDbContext = new DbContext(ServerDb);
}
public sealed class DbContext
{
private readonly SqlSugarScope _sqlSugarScope;
public DbContext(SqlSugarScope sqlSugarScope)
{
_sqlSugarScope = sqlSugarScope;
}
public void GenerateModels()
{
_sqlSugarScope.DbFirst.IsCreateAttribute().FormatFileName(it => "" + it).FormatClassName(it => "" + it).CreateClassFile("dbo", "ORMClsLib.dbo");
}
/// <summary>
/// 查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public List<T> Query<T>(Expression<Func<T, bool>> expression = null) where T : class
{
if (expression == null)
{
return _sqlSugarScope.Queryable<T>().ToList();
}
return _sqlSugarScope.Queryable<T>().Where(expression).ToList();
}
#region Delete
/// <summary>
/// 删除项
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item"></param>
public void Delete<T>(T item) where T : class, new()
{
_sqlSugarScope.Deleteable<T>(item).ExecuteCommand();
}
/// <summary>
/// 批量删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
public void Delete<T>(IEnumerable<T> list) where T : class, new()
{
_sqlSugarScope.Deleteable<T>(list).ExecuteCommand();
}
/// <summary>
/// 指定删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
public void Delete<T>(Expression<Func<T, bool>> expression) where T : class, new()
{
_sqlSugarScope.Deleteable<T>().Where(expression).ExecuteCommand();
}
/// <summary>
/// 主键删除
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="ids"></param>
public void Delete<T>(params int[] ids) where T : class, new()
{
_sqlSugarScope.Deleteable<T>().In(ids).ExecuteCommand();
}
#endregion
#region Add or Update
public void InsertOrUpdate<T>(List<T> list) where T : class, new()
{
// 大数据
if (list.Count > 1000)
{
_sqlSugarScope.Fastest<T>().BulkMerge(list);
}
else // 若主键递增 id=0 插入;!=0 更新
{
_sqlSugarScope.Storageable(list).DefaultAddElseUpdate().ExecuteCommand();
}
}
public void InsertOrUpdate<T>(T item) where T : class, new()
{
var x = _sqlSugarScope.Storageable(item).ToStorage();
x.AsInsertable.ExecuteCommand();
x.AsUpdateable.ExecuteCommand();
}
#endregion
}
}
namespace ORMClsLib.dbo
{
///<summary>
///
///</summary>
[SugarTable("DemoEntity")]
public partial class DemoEntity
{
public DemoEntity()
{
}
/// <summary>
/// Desc:
/// Default:
/// Nullable:False
/// </summary>
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int id { get; set; }
/// <summary>
/// Desc:
/// Default:
/// Nullable:True
/// </summary>
[SugarColumn(ColumnName = "operator")]
public string operatorName { get; set; }
}
}