mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-04-05 17:37:58 +08:00
Update demo
This commit is contained in:
parent
a996e8f483
commit
161d29c4a6
@ -0,0 +1,49 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoL_Snowflake
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### DemoL_Snowflake ####");
|
||||
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true,
|
||||
AopEvents = new AopEvents
|
||||
{
|
||||
OnLogExecuting = (sql, p) =>
|
||||
{
|
||||
Console.WriteLine(sql);
|
||||
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
|
||||
}
|
||||
}
|
||||
});
|
||||
db.CodeFirst.InitTables<SnowflakeModel>();
|
||||
Console.WriteLine(db.Queryable<SnowflakeModel>().Count());
|
||||
var id= db.Insertable(new SnowflakeModel()
|
||||
{
|
||||
Name="哈哈"
|
||||
}).ExecuteReturnSnowflakeId();
|
||||
var ids = db.Insertable(db.Queryable<SnowflakeModel>().Take(10).ToList()).ExecuteReturnSnowflakeIdList();
|
||||
Console.WriteLine(db.Queryable<SnowflakeModel>().Count());
|
||||
}
|
||||
}
|
||||
public class SnowflakeModel
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey =true)]
|
||||
public long Id { get; set; }
|
||||
|
||||
public string Name{get;set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoM_UnitOfWork
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### DemoM_UnitOfWork ####");
|
||||
|
||||
|
||||
DbContext.Db.UseTran(() =>
|
||||
{
|
||||
|
||||
var id = DbContext.CustomDal.InsertReturnIdentity(new Custom() { Id = 1, Name = "guid" });
|
||||
var id2 = DbContext.OrderDal.InsertReturnIdentity(new Order() { Name = "guid2", Price = 0, CreateTime = DateTime.Now, CustomId = 1 });
|
||||
throw new Exception("");
|
||||
},
|
||||
e =>
|
||||
{
|
||||
//throw e;
|
||||
});
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### Saveable End ####");
|
||||
}
|
||||
public class DbContext
|
||||
{
|
||||
public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
|
||||
{
|
||||
DbType = SqlSugar.DbType.SqlServer,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
IsAutoCloseConnection = true
|
||||
}, db => {
|
||||
//单例参数配置,所有上下文生效
|
||||
db.Aop.OnLogExecuting = (s, p) =>
|
||||
{
|
||||
Console.WriteLine(s);
|
||||
};
|
||||
});
|
||||
|
||||
public static DbSet<Order> OrderDal => new DbSet<Order>();
|
||||
public static DbSet<Custom> CustomDal => new DbSet<Custom>();
|
||||
|
||||
|
||||
public class DbSet<T> : SimpleClient<T> where T : class, new()
|
||||
{
|
||||
public DbSet(ISqlSugarClient context = null) : base(context)//需要有构造参数
|
||||
{
|
||||
base.Context = DbContext.Db;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扩展方法,自带方法不能满足的时候可以添加新方法
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<T> CommQuery(string json)
|
||||
{
|
||||
//base.Context.Queryable<T>().ToList();可以拿到SqlSugarClient 做复杂操作
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace OrmTest
|
||||
{
|
||||
public class DemoN_SplitTable
|
||||
{
|
||||
public static void Init()
|
||||
{
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("#### CodeFirst Start ####");
|
||||
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
|
||||
{
|
||||
DbType = DbType.SqlServer,
|
||||
ConnectionString = Config.ConnectionString,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
IsAutoCloseConnection = true
|
||||
});
|
||||
db.Aop.OnLogExecuted = (s, p) =>
|
||||
{
|
||||
Console.WriteLine(s);
|
||||
};
|
||||
|
||||
//初始化分表
|
||||
db.CodeFirst.SplitTables().InitTables<OrderSpliteTest>();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//根据最近3个表进行查询
|
||||
var list=db.Queryable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid())
|
||||
.SplitTable(tabs => tabs.Take(3))
|
||||
.Where(it=>it.Time==DateTime.Now).ToOffsetPage(1,2);
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//根据时间选出的表进行查询
|
||||
var list2 = db.Queryable<OrderSpliteTest>().SplitTable(tabs => tabs.Where(it=> it.Date>=DateTime.Now.AddYears(-2))).ToList();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//删除数据只在最近3张表执行操作
|
||||
var x = db.Deleteable<OrderSpliteTest>().Where(it=>it.Pk==Guid.NewGuid()).SplitTable(tabs => tabs.Take(3)).ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
var tableName = db.SplitHelper<OrderSpliteTest>().GetTableName(DateTime.Now.AddDays(-1));
|
||||
var tableName2 = db.SplitHelper(new OrderSpliteTest() { Time=DateTime.Now}).GetTableNames();
|
||||
var tableName3 = db.SplitHelper(new List<OrderSpliteTest> {
|
||||
new OrderSpliteTest() { Time = DateTime.Now },
|
||||
new OrderSpliteTest() { Time = DateTime.Now },
|
||||
new OrderSpliteTest() { Time = DateTime.Now.AddMonths(-10) }
|
||||
}).GetTableNames();
|
||||
var x2 = db.Updateable<OrderSpliteTest>()
|
||||
.SetColumns(it=>it.Name=="a")
|
||||
.Where(it => it.Pk == Guid.NewGuid())
|
||||
.SplitTable(tabs => tabs.InTableNames(tableName2))
|
||||
.ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
//按日分表
|
||||
var x3 = db.Insertable(new OrderSpliteTest() { Name="A" }).SplitTable().ExecuteCommand();
|
||||
|
||||
Console.WriteLine();
|
||||
////强制分表类型
|
||||
var x4 = db.Insertable(new OrderSpliteTest() { Name = "A" ,Time=DateTime.Now.AddDays(-1) }).SplitTable().ExecuteCommand();
|
||||
Console.WriteLine("#### CodeFirst end ####");
|
||||
}
|
||||
|
||||
[SplitTable(SplitType.Day)]
|
||||
[SqlSugar.SugarTable("Taxxx0101_{year}{month}{day}")]
|
||||
public class OrderSpliteTest
|
||||
{
|
||||
[SugarColumn(IsPrimaryKey =true)]
|
||||
public Guid Pk{ get; set; }
|
||||
public string Name { get; set; }
|
||||
[SugarColumn(IsNullable =true)]
|
||||
[SplitField]
|
||||
public DateTime Time { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
@ -19,6 +19,9 @@ namespace OrmTest
|
||||
Demo2_Updateable.Init();
|
||||
Demo3_Insertable.Init();
|
||||
Demo4_Deleteable.Init();
|
||||
DemoN_SplitTable.Init();
|
||||
DemoM_UnitOfWork.Init();
|
||||
DemoL_Snowflake.Init();
|
||||
Demo5_SqlQueryable.Init();
|
||||
Demo6_Queue.Init();
|
||||
Demo7_Ado.Init();
|
||||
|
Loading…
Reference in New Issue
Block a user