From bd9130a71a94f78ce5c76c4b007d886c1c6751a7 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 10 Jul 2025 15:56:13 +0800 Subject: [PATCH] Add demo --- .../UserTestCases/UnitTest/Main.cs | 1 + .../UserTestCases/UnitTest/Unitdsafasdy.cs | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdsafasdy.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs index 0faf511bf..606a42adf 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Main.cs @@ -33,6 +33,7 @@ namespace OrmTest } public static void Init() { + Unitdsfasdfys.Init(); Unitsadfasys.Init(); Unitsadfasys.Init(); Unitadfasfysdfyss.Init(); diff --git a/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdsafasdy.cs b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdsafasdy.cs new file mode 100644 index 000000000..3d3be98fe --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/UserTestCases/UnitTest/Unitdsafasdy.cs @@ -0,0 +1,85 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; +namespace OrmTest +{ + internal class Unitdsfasdfys + { + public static void Init() + { + ConnectionConfig config = new ConnectionConfig(); + config.ConnectionString =DbHelper.Connection; + config.ConfigId = "0"; + config.IsAutoCloseConnection = true; + config.DbType = SqlSugar.DbType.SqlServer; + config.MoreSettings = new ConnMoreSettings() + { + SqlServerCodeFirstNvarchar = true + }; + config.ConfigureExternalServices = new ConfigureExternalServices() + { + EntityService = (c, p) => + { + if (c.PropertyType == typeof(object) && p.DataType == "sql_variant") + { + p.SqlParameterDbType = SqlDbType.Variant; + } + if (p.IsPrimarykey == false && c.PropertyType.IsGenericType && c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))//自动可为空 + { + p.IsNullable = true; + } + if (p.PropertyName.ToLower() == "id" && p.IsPrimarykey)//默认Id这个为主键 + { + p.IsPrimarykey = true; + if (p.PropertyInfo.PropertyType == typeof(int)) + { + p.IsIdentity = true;//是id并且是int的是自增 + } + } + }, + EntityNameService = (type, entity) => + { + //entity.DbTableName 修改表名 + } + }; + SqlSugarScope sqlSugar = new SqlSugarScope(new List() { config }, + db => + { + db.GetConnectionScope("0").Aop.OnError = (exp) => + { + var sql = exp.Sql; + var parameters = exp.Parametres; + var str = $"0--SqlSugar异常 :{exp}"; + }; + db.GetConnectionScope("0").Aop.OnLogExecuting = (sql, pars) => + { + string msg = $"0--SqlSugar 执行了Sql语句:{sql}"; + }; + }); + sqlSugar.GetConnectionScope("0").DbMaintenance.CreateDatabase(); + var Db = sqlSugar.AsTenant().GetConnectionScope("0"); + Db.CodeFirst.InitTables(new Type[] { typeof(TestDateTime) }); + Db.DbMaintenance.TruncateTable(); + TestDateTime testDateTime = new TestDateTime() + { + CreateTime = DateTime.Parse("2025-03-29 09:27:37.9991749") + }; + Db.Insertable(testDateTime).ExecuteCommand(); + + var data = Db.Queryable().First().CreateTime.ToString("yyyy-MM-dd HH:mm:ss.fffffff"); + if (data != "2025-03-29 09:27:37.9991749") throw new Exception("unit error"); + } + } + public class TestDateTime + { + [SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + [SugarColumn(SqlParameterDbType=System.Data.DbType.DateTime2 , ColumnDataType = "datetime2(7)")] + public DateTime CreateTime { get; set; } + } +}