Update Demo

This commit is contained in:
sunkaixuan
2019-05-06 14:20:43 +08:00
parent 5c22af3b2e
commit 1828117af6
2 changed files with 214 additions and 119 deletions

View File

@@ -13,11 +13,66 @@ namespace OrmTest
public static void Init()
{
SqlSugarClient();
DbContext();//使用DbContext美化代码
SingletonPattern();//单例
DistributedTransactionExample();//分布式事务
CustomAttribute();//自定义特性
}
private static void SqlSugarClient()
{
}
private static void DbContext()
{
var insertObj = new Order { Name = "jack", CreateTime=DateTime.Now };
var InsertObjs = new Order[] { insertObj };
DbContext context = new DbContext();
context.Db.CodeFirst.InitTables<Order, OrderItem>();//Create Tables
;
var orderDb = context.OrderDb;
//Select
var data1 = orderDb.GetById(1);
var data2 = orderDb.GetList();
var data3 = orderDb.GetList(it => it.Id == 1);
var data4 = orderDb.GetSingle(it => it.Id == 1);
var p = new PageModel() { PageIndex = 1, PageSize = 2 };
var data5 = orderDb.GetPageList(it => it.Name == "xx", p);
Console.Write(p.PageCount);
var data6 = orderDb.GetPageList(it => it.Name == "xx", p, it => it.Name, OrderByType.Asc);
Console.Write(p.PageCount);
List<IConditionalModel> conModels = new List<IConditionalModel>();
conModels.Add(new ConditionalModel() { FieldName = "id", ConditionalType = ConditionalType.Equal, FieldValue = "1" });//id=1
var data7 = orderDb.GetPageList(conModels, p, it => it.Name, OrderByType.Asc);
orderDb.AsQueryable().Where(x => x.Id == 1).ToList();
//Insert
orderDb.Insert(insertObj);
orderDb.InsertRange(InsertObjs);
var id = orderDb.InsertReturnIdentity(insertObj);
orderDb.AsInsertable(insertObj).ExecuteCommand();
//Delete
orderDb.Delete(insertObj);
orderDb.DeleteById(1);
orderDb.DeleteById(new int[] { 1, 2 });
orderDb.Delete(it => it.Id == 1);
orderDb.AsDeleteable().Where(it => it.Id == 1).ExecuteCommand();
//Update
orderDb.Update(insertObj);
orderDb.UpdateRange(InsertObjs);
orderDb.Update(it => new Order() { Name = "a", }, it => it.Id == 1);
orderDb.AsUpdateable(insertObj).UpdateColumns(it=>new { it.Name }).ExecuteCommand();
}
private static void CustomAttribute()
{
Console.WriteLine("");
@@ -91,7 +146,7 @@ namespace OrmTest
DbType = DbType.SqlServer,
ConnectionString = Config.ConnectionString,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection=true,
IsAutoCloseConnection = true,
AopEvents = new AopEvents()
{
OnLogExecuting = (sql, p) => { Console.WriteLine(sql); }
@@ -164,4 +219,30 @@ namespace OrmTest
}
public class DbContext
{
public SqlSugarClient Db;
public DbContext()
{
Db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute,
AopEvents = new AopEvents()
{
OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
}
}
});
}
public SimpleClient<Order> OrderDb => new SimpleClient<Order>(Db);
public SimpleClient<OrderItem> OrderItemDb => new SimpleClient<OrderItem>(Db);
}
}

View File

@@ -6,131 +6,22 @@ using System.Text;
namespace SqlSugar
{
public partial class SimpleClient
{
protected SqlSugarContext Context { get; set; }
public SqlSugarContext FullClient { get { return this.Context; } }
private SimpleClient()
{
}
public SimpleClient(SqlSugarContext context)
{
this.Context = context;
}
public T GetById<T>(dynamic id) where T : class, new()
{
return Context.Queryable<T>().InSingle(id);
}
public int Count<T>(Expression<Func<T, bool>> whereExpression)
{
return Context.Queryable<T>().Where(whereExpression).Count();
}
public List<T> GetList<T>() where T : class, new()
{
return Context.Queryable<T>().ToList();
}
public T GetSingle<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return Context.Queryable<T>().Single(whereExpression);
}
public List<T> GetList<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return Context.Queryable<T>().Where(whereExpression).ToList();
}
public List<T> GetPageList<T>(Expression<Func<T, bool>> whereExpression, PageModel page) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public List<T> GetPageList<T>(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public List<T> GetPageList<T>(List<IConditionalModel> conditionalList, PageModel page) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public List<T> GetPageList<T>(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public bool IsAny<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return Context.Queryable<T>().Where(whereExpression).Any();
}
public bool Insert<T>(T insertObj) where T : class, new()
{
return this.Context.Insertable(insertObj).ExecuteCommand() > 0;
}
public int InsertReturnIdentity<T>(T insertObj) where T : class, new()
{
return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
}
public bool InsertRange<T>(T[] insertObjs) where T : class, new()
{
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
}
public bool InsertRange<T>(List<T> insertObjs) where T : class, new()
{
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
}
public bool Update<T>(T updateObj) where T : class, new()
{
return this.Context.Updateable(updateObj).ExecuteCommand() > 0;
}
public bool UpdateRange<T>(T[] updateObjs) where T : class, new()
{
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
}
public bool UpdateRange<T>(List<T> updateObjs) where T : class, new()
{
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
}
public bool Update<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return this.Context.Updateable<T>(columns).Where(whereExpression).ExecuteCommand() > 0;
}
public bool Delete<T>(T deleteObj) where T : class, new()
{
return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
}
public bool Delete<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
}
public bool DeleteById<T>(dynamic id) where T : class, new()
{
return this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
}
public bool DeleteByIds<T>(dynamic[] ids) where T : class, new()
{
return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
}
}
public partial class SimpleClient<T> where T : class, new()
{
protected SqlSugarContext Context { get; set; }
public SqlSugarContext FullClient { get { return this.Context; } }
protected ISqlSugarClient Context { get; set; }
[Obsolete("Use AsSugarClient()")]
public ISqlSugarClient FullClient { get { return this.Context; } }
public ISqlSugarClient AsSugarClient()
{
return this.Context;
}
private SimpleClient()
{
}
public SimpleClient(SqlSugarContext context)
public SimpleClient(ISqlSugarClient context)
{
this.Context = context;
}
@@ -272,4 +163,127 @@ namespace SqlSugar
return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
}
}
public partial class SimpleClient
{
protected ISqlSugarClient Context { get; set; }
[Obsolete("Use AsSugarClient()")]
public ISqlSugarClient FullClient { get { return this.Context; } }
public ISqlSugarClient AsSugarClient()
{
return this.Context;
}
private SimpleClient()
{
}
public SimpleClient(ISqlSugarClient context)
{
this.Context = context;
}
public T GetById<T>(dynamic id) where T : class, new()
{
return Context.Queryable<T>().InSingle(id);
}
public int Count<T>(Expression<Func<T, bool>> whereExpression)
{
return Context.Queryable<T>().Where(whereExpression).Count();
}
public List<T> GetList<T>() where T : class, new()
{
return Context.Queryable<T>().ToList();
}
public T GetSingle<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return Context.Queryable<T>().Single(whereExpression);
}
public List<T> GetList<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return Context.Queryable<T>().Where(whereExpression).ToList();
}
public List<T> GetPageList<T>(Expression<Func<T, bool>> whereExpression, PageModel page) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public List<T> GetPageList<T>(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public List<T> GetPageList<T>(List<IConditionalModel> conditionalList, PageModel page) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public List<T> GetPageList<T>(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc) where T : class, new()
{
int count = 0;
var result = Context.Queryable<T>().OrderByIF(orderByExpression != null, orderByExpression, orderByType).Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
page.PageCount = count;
return result;
}
public bool IsAny<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return Context.Queryable<T>().Where(whereExpression).Any();
}
public bool Insert<T>(T insertObj) where T : class, new()
{
return this.Context.Insertable(insertObj).ExecuteCommand() > 0;
}
public int InsertReturnIdentity<T>(T insertObj) where T : class, new()
{
return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
}
public bool InsertRange<T>(T[] insertObjs) where T : class, new()
{
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
}
public bool InsertRange<T>(List<T> insertObjs) where T : class, new()
{
return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
}
public bool Update<T>(T updateObj) where T : class, new()
{
return this.Context.Updateable(updateObj).ExecuteCommand() > 0;
}
public bool UpdateRange<T>(T[] updateObjs) where T : class, new()
{
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
}
public bool UpdateRange<T>(List<T> updateObjs) where T : class, new()
{
return this.Context.Updateable(updateObjs).ExecuteCommand() > 0;
}
public bool Update<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return this.Context.Updateable<T>(columns).Where(whereExpression).ExecuteCommand() > 0;
}
public bool Delete<T>(T deleteObj) where T : class, new()
{
return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
}
public bool Delete<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
{
return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
}
public bool DeleteById<T>(dynamic id) where T : class, new()
{
return this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
}
public bool DeleteByIds<T>(dynamic[] ids) where T : class, new()
{
return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
}
}
}