SqlSugar .Net ORM
//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;
}
/// <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();
}
/// <summary>
/// 查询最值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="maxField">列名称</param>
/// <returns></returns>
public T QueryMax<T>(string maxField)
{
return _sqlSugarScope.Queryable<T>().Max<T>(maxField);
}
/// <summary>
/// 查询一条
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public T QueryFirst<T>(Expression<Func<T, bool>> expression = null) where T : class
{
return _sqlSugarScope.Queryable<T>().First(expression);
}
/// <summary>
/// 去重查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public List<TResult> QueryDistinct<T, TResult>(Expression<Func<T, TResult>> expression)
{
return _sqlSugarScope.Queryable<T>().Select(expression).Distinct().ToList();
}
/// <summary>
/// 按正序查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public List<T> QueryOrder<T>(Expression<Func<T, object>> expression = null) where T : class
{
return _sqlSugarScope.Queryable<T>().OrderBy(expression).ToList();
}
/// <summary>
/// 按降序查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="expression"></param>
/// <returns></returns>
public List<T> QueryByDescending<T>(Expression<Func<T, object>> expression = null) where T : class
{
return _sqlSugarScope.Queryable<T>().OrderByDescending(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 bool 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();
//}
return _sqlSugarScope.Fastest<T>().BulkUpdate(list) == list.Count;
}
public bool InsertOrUpdate<T>(T item) where T : class, new()
{
var x = _sqlSugarScope.Storageable(item).ToStorage();
int rslt = x.AsInsertable.ExecuteCommand();
int rslt2 = x.AsUpdateable.ExecuteCommand();
return rslt + rslt2 > 1;
}
#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; }
}
}