SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.6 KiB

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrmTest
{
public class UnitCustom01
{
public static void Init()
{
var ssc = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = OrmTest.Config.ConnectionString,
DbType = SqlSugar.DbType.MySql, //必填
IsAutoCloseConnection = true
});
ssc.Aop.OnLogExecuting = (s, p) =>
{
Console.WriteLine(s);
};
ssc.CodeFirst.InitTables<Student>();
var expMethods = new List<SqlFuncExternal>();
expMethods.Add(new SqlFuncExternal()
{
UniqueMethodName = "SumSugar",
MethodValue = (expInfo, dbType, expContext) =>
{
if (dbType == DbType.SqlServer)
return string.Format("SUM({0})", expInfo.Args[0].MemberName);
else if (dbType == DbType.MySql)
return string.Format("SUM({0})", expInfo.Args[0].MemberName);
else
throw new Exception("未实现");
}
});
ssc.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices()
{
SqlFuncServices = expMethods //set ext method
};
try
{
ssc.Insertable(new Student() { Age = 1, Createtime = DateTime.Now, Grade = 1, Id = 1, Name = "a", Schoolid = 1 }).ExecuteCommand();
ssc.Insertable(new Student() { Age = 1, Createtime = DateTime.Now, Grade = 1, Id = 1, Name = "a", Schoolid = 1 }).ExecuteCommand();
var sss12 = ssc.Queryable<Student>().GroupBy(o => o.Name).Select(o => new { Age = SqlFunc.AggregateSum(o.Age) }).ToList();
}
catch (Exception e)
{
}
ssc.CodeFirst.InitTables<unittest1231sdaa>();
ssc.CodeFirst.InitTables<unittest1231sdaa2>();
}
}
public class unittest1231sdaa
{
[SugarColumn(IndexGroupNameList = new string[] { "index3" }, ColumnDataType = "DATETIME(6)", DefaultValue = "CURRENT_TIMESTAMP(6)")]
public DateTime update_time { get; set; }
}
public class unittest1231sdaa2
{
[SugarColumn(IndexGroupNameList = new string[] { "index3" }, ColumnDataType = "DATETIME", DefaultValue = "CURRENT_TIMESTAMP")]
public DateTime update_time { get; set; }
}
[SugarTable("unitstudent1111")]
public class Student
{
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "id", IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "schoolid")]
public int? Schoolid { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "name")]
public string Name { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "createtime")]
public DateTime? Createtime { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "age")]
public int? Age { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName = "grade")]
public int? Grade { get; set; }
}
}