Browse Source

Update Core Demo

pull/12/MERGE
sunkaixuan 6 years ago
parent
commit
d3e00438ee
  1. 101
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/BugTest/Bug1.cs
  2. 6
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Config.cs
  3. 34
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs
  4. 5
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs
  5. 9
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs
  6. 7
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs
  7. 20
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs
  8. 51
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs
  9. 58
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/H_ExtEntity.cs
  10. 37
      Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs

101
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/BugTest/Bug1.cs

@ -0,0 +1,101 @@
using OrmTest.Demo;
using OrmTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.BugTest
{
public class Bug1
{
public static SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
InitKeyType = InitKeyType.Attribute,
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true
});
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
return db;
}
public void Init()
{
var communityId = "";
var buildId = "";
var unitId = "";
var keyword = "";
GetInstance().CodeFirst.InitTables(typeof(MainTable), typeof(SubTable));
GetInstance().Queryable<MainTable>().Where(u =>
(u.CommunityID == communityId || SqlFunc.IsNullOrEmpty(communityId)) &&
(SqlFunc.Contains(u.BuildID, buildId) || SqlFunc.IsNullOrEmpty(buildId)) &&
(SqlFunc.Contains(u.UnitID, unitId) || SqlFunc.IsNullOrEmpty(unitId)) &&
(SqlFunc.Contains(u.RoomNumber, keyword) || SqlFunc.Contains(u.RoomerName, keyword) ||
SqlFunc.Contains(u.HousePlace, keyword) || SqlFunc.Contains(u.UnitName, keyword) ||
SqlFunc.Contains(u.BuildName, keyword) || SqlFunc.IsNullOrEmpty(keyword)))
.GroupBy(ru => new { ru.RoomNumber, ru.RoomID })
.Select(ru => new
{
RoomNumber = SqlFunc.AggregateMax(ru.RoomNumber),
CountRoomer = SqlFunc.AggregateCount(ru.RoomerName),
RoomID = SqlFunc.AggregateMax(ru.RoomID),
Owner = SqlFunc.Subqueryable<SubTable>().Where(r => r.RoomID == ru.RoomID && SqlFunc.Equals(r.RoomUserType, "业主") && SqlFunc.Equals(r.RoomUserType, "业主")).Select(s => s.RoomerName)
}).OrderBy((r) => r.RoomNumber, type: OrderByType.Desc).ToPageListAsync(1, 2).Wait();
GetInstance().Updateable<Student>().UpdateColumns(it =>
new Student()
{
Name = "a".ToString(),
CreateTime = DateTime.Now.AddDays(-1)
}
).Where(it => it.Id == 1).ExecuteCommand();
var list = GetInstance().Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id&&st.CreateTime==DateTime.Now.AddDays(-1)
})
.Where(st => st.Name == "jack").ToList();
GetInstance().Updateable<BugStudent>().Where(it => true).UpdateColumns(it => new BugStudent() { Float = 11 }).ExecuteCommand();
var reslut= GetInstance().Queryable<BugStudent>().ToList();
}
[SugarTable("student")]
public class BugStudent
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
public string name { get; set; }
public float? Float { get; set; }
}
public class MainTable
{
public string CommunityID { get; internal set; }
public string BuildID { get; internal set; }
public string UnitID { get; internal set; }
public string RoomNumber { get; internal set; }
public string HousePlace { get; internal set; }
public string BuildName { get; internal set; }
public string RoomID { get; internal set; }
public string UnitName { get; internal set; }
public string RoomerName { get; internal set; }
}
public class SubTable
{
public string RoomID { get; internal set; }
public string RoomUserType { get; internal set; }
public string RoomerName { get; internal set; }
}
}
}

6
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Config.cs

@ -8,8 +8,8 @@ namespace OrmTest
{
public class Config
{
public static string ConnectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugar4XTest";
public static string ConnectionString2 = "server=.;uid=sa;pwd=sasa;database=SQLSUGAR4XTEST";
public static string ConnectionString3 = "server=.;uid=sa;pwd=sasa;database=sqlsugar4xtest";
public static string ConnectionString = "server=.;uid=sa;pwd=@jhl85661501;database=SqlSugar4XTest";
public static string ConnectionString2 = "server=.;uid=sa;pwd=@jhl85661501;database=SQLSUGAR4XTEST";
public static string ConnectionString3 = "server=.;uid=sa;pwd=@jhl85661501;database=sqlsugar4xtest";
}
}

34
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/1_Query.cs

@ -94,6 +94,27 @@ namespace OrmTest.Demo
Id = SqlFunc.Subqueryable<School>().Where(s => s.Id == it.Id).Select(s => s.Id)
}).ToList();
var getAll6 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().Where(s => s.Id == it.Id).Sum(s => (int)s.SchoolId)
}).ToList();
var getAll66 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().Where(s => s.Id == it.Id).Sum(s =>s.SchoolId.Value)
}).ToList();
var getAll666 = db.Queryable<Student>().Select(it =>
new
{
name = it.Name,
id = SqlFunc.Subqueryable<Student>().Where(s => s.Id == it.Id).Min(s => s.Id)
}).ToList();
}
private static void Async()
@ -318,6 +339,12 @@ namespace OrmTest.Demo
var getUnionAllList2 = db.UnionAll(db.Queryable<Student>(), db.Queryable<Student>()).ToList();
var getUnionAllList3= db.UnionAll(db.Queryable<Student>()
.Select(it => new Student { Id =SqlFunc.ToInt32(1) ,Name=SqlFunc.ToString("2"), SchoolId = Convert.ToInt32(3) })
, db.Queryable<Student>()
.Select(it => new Student { Id = SqlFunc.ToInt32(11) , Name = SqlFunc.ToString("22") , SchoolId=Convert.ToInt32(33)}))
.Select(it=>new Student() { Id=SqlFunc.ToInt32(111), Name = SqlFunc.ToString("222") }).ToList();
var test1 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Where(st=>st.CreateTime>SqlFunc.GetDate()).Select((st, sc) => SqlFunc.ToInt64(sc.Id)).ToList();
var test2 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
.Where(st =>
@ -473,6 +500,13 @@ namespace OrmTest.Demo
.OrderBy(st => st.Id)
.Select(st => st)
.ToList();
var subquery = db.Queryable<Student>().Where(it => it.Id == 1);
var subquery2 = db.Queryable<Student>();
db.Queryable(subquery, subquery2, (st1, st2) => st1.Id == st2.Id).Select((st1,st2)=>new {
id=st1.Id,
name=st2.Name
}).ToList();
}
public static void Funs()
{

5
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/2_Update.cs

@ -68,6 +68,11 @@ namespace OrmTest.Demo
//update one columns
var count = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId).Where(it => it.Id == it.Id+1).ExecuteCommand();
var count1 = db.Updateable<Student>()
.UpdateColumnsIF(false,it => it.SchoolId == it.SchoolId)//ignore
.UpdateColumnsIF(true, it => it.SchoolId == 2).//ok
Where(it => it.Id == it.Id + 1).ExecuteCommand();
//update one columns
var count2 = db.Updateable<Student>().UpdateColumns(it => it.SchoolId == it.SchoolId+1).Where(it => it.Id == it.Id + 1).ExecuteCommand();

9
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/4_Delete.cs

@ -25,6 +25,15 @@ namespace OrmTest.Demo
//by primary key array
var t4 = db.Deleteable<Student>().In(new int[] { 1, 2 }).ExecuteCommand();
var t41 = db.Deleteable<Student>().In(new int[] { 1, 2 }.Select(it=>it)).ExecuteCommand();
var t42 = db.Deleteable<Student>().In(new int[] { 1, 2 }.AsEnumerable()).ExecuteCommand();
//by exp key array
var t44 = db.Deleteable<Student>().In(it=>it.SchoolId,new int[] { 1, 2 }).ExecuteCommand();
var t441 = db.Deleteable<Student>().In(it => it.SchoolId,new int[] { 1, 2 }.Select(it => it)).ExecuteCommand();
var t442 = db.Deleteable<Student>().In(it => it.SchoolId,new int[] { 1, 2 }.AsEnumerable()).ExecuteCommand();
var t443 = db.Deleteable<Student>().In(it => it.SchoolId, new int[] { 1, 2 }.ToList()).ExecuteCommand();
//by expression id>1 and id==1
var t5 = db.Deleteable<Student>().Where(it => it.Id > 1).Where(it => it.Id == 1).ExecuteCommand();

7
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/5_CodeFirst.cs

@ -6,14 +6,15 @@ using System.Text;
namespace OrmTest.Demo
{
[SugarTable("CodeTable", " table CodeTable")]
public class CodeTable
{
[SugarColumn(IsNullable =false ,IsPrimaryKey =true,IsIdentity =true)]
[SugarColumn(IsNullable =false ,IsPrimaryKey =true,IsIdentity =true,ColumnDescription ="XXhaha primary key!!")]
public int Id { get; set; }
[SugarColumn(Length = 21,OldColumnName = "Name2")]
public string Name{ get; set; }
[SugarColumn(IsNullable = true,Length =10)]
public string IsOk { get; set; }
public Guid Guid { get; set; }
[SugarColumn(ColumnDataType ="int")]
@ -49,7 +50,7 @@ namespace OrmTest.Demo
//db.CodeFirst.BackupTable().InitTables(typeof(CodeTable),typeof(CodeTable2));
//No backup table
db.CodeFirst.InitTables(typeof(CodeTable),typeof(CodeTable2));
db.CodeFirst.SetStringDefaultLength(10).InitTables(typeof(CodeTable),typeof(CodeTable2));
}
}
}

20
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/8_JoinSql.cs

@ -20,6 +20,26 @@ namespace OrmTest.Demo
SelectMerge();
ConditionalModel();
JoinExp();
Clone();
}
private static void Clone()
{
var db = GetInstance();
var qy = db.Queryable<Student>().Where(it => 1 == 1);
var list1 = qy.Clone().Where(it => it.Id == 1).ToList();
var list2 = qy.Clone().Where(it => it.Id == 2).ToList();
var qy2 = db.Queryable<Student,School>((st,sc)=>new object[]{
JoinType.Left,st.SchoolId==sc.Id
}).Where((st,sc)=>st.Id == 1);
var join0 = qy2.Clone().Where((st, sc) => sc.Id == 222).Select(st=>st.Id).ToList();
var join1 = qy2.Clone().Where((st,sc) => st.Id== 1111).ToList();
var join2 = qy2.Clone().Where((st,sc)=>sc.Id==222).ToList();
}
private static void JoinExp()

51
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/9_Aop.cs

@ -1,4 +1,5 @@
using SqlSugar;
using OrmTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
@ -12,9 +13,11 @@ namespace OrmTest.Demo
public static void Init()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Aop.OnLogExecuted = (sql, pars) =>
{
Console.Write("time:" + db.Ado.SqlExecutionTime.ToString());
};
db.Aop.OnLogExecuting = (sql, pars) =>
{
@ -26,7 +29,7 @@ namespace OrmTest.Demo
};
db.Aop.OnExecutingChangeSql = (sql, pars) =>
{
return new KeyValuePair<string, SugarParameter[]>(sql,pars);
return new KeyValuePair<string, SugarParameter[]>(sql, pars);
};
db.Queryable<CMStudent>().ToList();
@ -40,7 +43,45 @@ namespace OrmTest.Demo
}
}
}
//diff log demo
db.Aop.OnDiffLogEvent = it =>
{
var editBeforeData = it.BeforeData;
var editAfterData = it.AfterDate;
var sql = it.Sql;
var parameter = it.Parameters;
var data = it.BusinessData;
};
var id = db.Insertable(new Student() { Name = "beforeName" })
.EnableDiffLogEvent(new { title="add student"})
.ExecuteReturnIdentity();
db.Updateable<Student>(new Student()
{
Id = id,
CreateTime = DateTime.Now,
Name = "afterName",
SchoolId = 2
})
.EnableDiffLogEvent(new { title = "update Student", Modular = 1, Operator = "admin" })
.ExecuteCommand();
db.Deleteable<Student>(id)
.EnableDiffLogEvent(new { title = "delete student" })
.ExecuteCommand();
//primary key guid
db.Insertable(new DataTestInfo2() { Bool1=true, Bool2=false, PK=Guid.NewGuid(), Text1="a" })
.EnableDiffLogEvent(new { title = "add DataTestInfo2" })
.ExecuteReturnIdentity();
}
}
}

58
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/H_ExtEntity.cs

@ -0,0 +1,58 @@
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
namespace OrmTest.Demo
{
/// <summary>
/// mapping ef attribute
/// </summary>
public class ExtEntity: DemoBase
{
public static void Init()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() {
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
IsAutoCloseConnection = true,
ConfigureExternalServices=new ConfigureExternalServices() {
EntityService = (property, column) => {
if (property.Name == "xxx") {// by name ignore column
column.IsIgnore = true;
}
var attributes = property.GetCustomAttributes(true);//get all attributes
if (attributes.Any(it => it is KeyAttribute))// by attribute set primarykey
{
column.IsPrimarykey = true;
}
},
EntityNameService = (type,entity) => {
var attributes = type.GetCustomAttributes(true);
if (attributes.Any(it => it is TableAttribute))
{
entity.DbTableName = (attributes.First(it => it is TableAttribute)as TableAttribute).Name;
}
}
}
});
var sql=db.Queryable<StudentTest>().ToList();
var sql2 = db.Insertable<StudentTest>(new StudentTest()).ExecuteCommand();
}
}
[Table("student")]//default
public class StudentTest {
[Key]
public string Id { get; set; }
public string xxx { get; set; }
public string Name { get; set; }
}
}

37
Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs

@ -17,6 +17,7 @@ namespace OrmTest
{
static void Main(string[] args)
{
// /***Unit Test***/
new Select(1).Init();
new Field(1).Init();
@ -36,22 +37,26 @@ namespace OrmTest
new SqlSugarPerformance(100).Select();
/***Demo***/
OrmTest.Demo.Query.Init();
OrmTest.Demo.Insert.Init();
OrmTest.Demo.Delete.Init();
OrmTest.Demo.Update.Init();
OrmTest.Demo.DbFirst.Init();
OrmTest.Demo.JoinSql.Init();
OrmTest.Demo.Filter.Init();
OrmTest.Demo.ComplexModel.Init();
OrmTest.Demo.CodeFirst.Init();
OrmTest.Demo.Aop.Init();
OrmTest.Demo.MasterSlave.Init();
OrmTest.Demo.SharedConnection.Init();
OrmTest.Demo.ExtSqlFun.Init();
OrmTest.Demo.QueryableView.Init();
OrmTest.Demo.Mapper.Init();
//OrmTest.Demo.VersionValidation.Init();
Demo.Query.Init();
Demo.Insert.Init();
Demo.Delete.Init();
Demo.Update.Init();
Demo.DbFirst.Init();
Demo.JoinSql.Init();
Demo.Filter.Init();
Demo.ComplexModel.Init();
Demo.CodeFirst.Init();
Demo.Aop.Init();
Demo.MasterSlave.Init();
Demo.SharedConnection.Init();
Demo.ExtSqlFun.Init();
Demo.QueryableView.Init();
Demo.AttributeDemo.Init();
Demo.Mapper.Init();
Demo.ExtEntity.Init();
/***BUG repair test***/
new BugTest.Bug1().Init();
}
}
}

Loading…
Cancel
Save