zoukankan      html  css  js  c++  java
  • Ef Core增加Sql方法

        [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
        public class DbFunAttribute : Attribute
        {
            public DbFunAttribute( string functionName, Type returnType)
            {
                ReturnType = returnType;
                FunctionName = functionName;
            }
    
            public DbFunAttribute()
            {
    
            }
    
            public Type ReturnType { get; set; }
            public string FunctionName { get; set; }
        }
    
        public static class SqlFunctionExtension
        {
            public static ModelBuilder UseSqlFunction(this ModelBuilder modelBuilder)
            {
                foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    foreach (var type in assembly.GetTypes()
                    .Where(item=>item.GetCustomAttribute<DbFunAttribute>() != null))
                    {
                        List<MethodInfo> funList = type.GetMethods(BindingFlags.Public | BindingFlags.Static)
                            .Where(method => method.GetCustomAttribute<DbFunAttribute>() != null)
                            .ToList();
    
                        foreach (var method in funList)
                        {
                            modelBuilder.HasDbFunction(method).HasTranslation(arg =>
                            {
                                var argumentos = arg.ToList();
                                var attribute = method.GetCustomAttribute<DbFunAttribute>();
                                var funcionName = attribute.FunctionName;
                                return new SqlFunctionExpression(
                                    funcionName,
                                    attribute.ReturnType,
                                    argumentos);
                            });
                        }
                    }
                }
    
                return modelBuilder;
            }
        }

    调用的Sql方法

        [DbFun]
        public class Function
        {
            public static int DateDiff(string part, DateTime inicio, DateTime fim)
                => 0;
    
            [DbFun("REPLACE", typeof(string))]
            public static string Replace(object dados, string substituir, string por)
                => string.Empty;
        }

    启用

    Ef的DbContext对象下面的

            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
    
                modelBuilder.HasDbFunction(typeof(Function).GetMethod("DateDiff")).HasTranslation((arg =>
                {
                    var argumentos = arg.ToList();
                    argumentos[0] = new SqlFragmentExpression((string)((ConstantExpression)argumentos.First()).Value);
                    return new SqlFunctionExpression(
                        "DATEDIFF",
                        typeof(int),
                        argumentos);
                }));
    
                modelBuilder.UseSqlFunction();
    }

    调用

                var serviceProvider = services.BuildServiceProvider();
                var context = serviceProvider.GetService(typeof(CoreWebContext)) as CoreWebContext;
    
                var query = context.EntityOrder.Where(item => Function.DateDiff("MONTH", DateTime.Parse("2018-06-01"), item.PayTime.Value) == 0).Select(item =>
                    Function.Replace(item.OrderNo, "2018", "xyzx")
                );
                var list = query.ToList();
  • 相关阅读:
    黄页js-sdk开发总结分享
    最近的shell脚本(updating)
    nginx location 的配置
    nodejs 的安全
    paypal之nodejs 框架 Kraken-js 源码分析
    nodejs express 框架解密5-视图
    nodejs express 框架解密4-路由
    nodejs express 框架解密3-中间件模块
    nodejs express 框架解密2-如何创建一个app
    nodejs express 框架解密1-总体结构
  • 原文地址:https://www.cnblogs.com/NCoreCoder/p/9172143.html
Copyright © 2011-2022 走看看