diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/C_ExtSqlFun.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/C_ExtSqlFun.cs new file mode 100644 index 000000000..f0b4c0580 --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Demos/C_ExtSqlFun.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using SqlSugar; +using OrmTest.Models; + +namespace OrmTest.Demo +{ + public class ExtSqlFun : DemoBase + { + public static SqlSugarClient GetDb() + { + //Create ext method + var expMethods = new List(); + expMethods.Add(new SqlFuncExternal() + { + UniqueMethodName = "MyToString", + MethodValue = (expInfo, dbType, expContext) => + { + return string.Format("CAST({0} AS VARCHAR(MAX))", expInfo.Args[0].MemberName); + } + }); + + var config = new ConnectionConfig() + { + ConnectionString = Config.ConnectionString, + DbType = DbType.SqlServer, + IsAutoCloseConnection = true, + ConfigureExternalServices = new ConfigureExternalServices() + { + SqlFuncServices = expMethods//set ext method + } + }; + + SqlSugarClient db = new SqlSugarClient(config); + return db; + } + + public static string MyToString(T str) + { + throw new NotSupportedException("Can only be used in expressions"); + } + + public static void Init() + { + var db = GetDb(); + var list = db.Queryable().Where(it => MyToString(it.Id) == "1302583").ToList(); + var sql = db.Queryable().Where(it => MyToString(it.Id) == "1302583").ToSql(); + Console.WriteLine(sql); + } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs index 8fad6514c..bf77f7c1e 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSeverTest/Program.cs @@ -39,6 +39,7 @@ namespace SqlSeverTest OrmTest.Demo.CodeFirst.Init(); OrmTest.Demo.MasterSlave.Init(); OrmTest.Demo.SharedConnection.Init(); + OrmTest.Demo.ExtSqlFun.Init(); } } }