/// <summary>
        /// FreeSql
        /// </summary>
        /// <param name="services"></param>
        public static void AddContext(this IServiceCollection services, IConfiguration configuration)
        {
            IConfigurationSection configurationSection = configuration.GetSection("ConnectionStrings:MySql");


            IFreeSql fsql = new FreeSqlBuilder()
                            .UseConnectionString(DataType.MySql, configurationSection.Value)
                            .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
                            .UseAutoSyncStructure(true)
                            .UseNoneCommandParameter(true)
                            .UseMonitorCommand(cmd =>
            {
                Trace.WriteLine(cmd.CommandText + ";");
            }
                                               )
                            .Build()
                            .SetDbContextOptions(opt => opt.EnableAddOrUpdateNavigateList = true);//联级保存功能开启(默认为关闭)



            fsql.Aop.CurdAfter += (s, e) =>
            {
                Log.Debug($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}: FullName:{e.EntityType.FullName}" +
                          $" ElapsedMilliseconds:{e.ElapsedMilliseconds}ms, {e.Sql}");

                if (e.ElapsedMilliseconds > 200)
                {
                    //记录日志
                    //发送短信给负责人
                }
            };

            //敏感词处理
            if (configuration["AuditValue:Enable"].ToBoolean())
            {
                IllegalWordsSearch illegalWords = ToolGoodUtils.GetIllegalWordsSearch();

                fsql.Aop.AuditValue += (s, e) =>
                {
                    if (e.Column.CsType == typeof(string) && e.Value != null)
                    {
                        string oldVal = (string)e.Value;
                        string newVal = illegalWords.Replace(oldVal);
                        //第二种处理敏感词的方式
                        //string newVal = oldVal.ReplaceStopWords();
                        if (newVal != oldVal)
                        {
                            e.Value = newVal;
                        }
                    }
                };
            }

            services.AddSingleton(fsql);
            services.AddScoped <UnitOfWorkManager>();
            fsql.GlobalFilter.Apply <IDeleteAduitEntity>("IsDeleted", a => a.IsDeleted == false);
            //在运行时直接生成表结构
            fsql.CodeFirst.SyncStructure(ReflexHelper.GetEntityTypes(typeof(IEntity)));
            services.AddFreeRepository();
        }
Ejemplo n.º 2
0
        public void RelfexGetCustomAttributes()
        {
            var entityTypes = ReflexHelper.GetEntityTypes(typeof(IEntity));

            Assert.True(entityTypes.Length > 0);
        }