using Furion; using Furion.Logging.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Xml.Linq; namespace CYZuoYeBenPeiSong.Core { public class Startup : AppStartup { //使用sqlsugar 数据库连接库 public void ConfigureServices(IServiceCollection services) { var configConnection = App.GetConfig>("DbConnect"); services.AddSingleton(s => { SqlSugarScope sqlSugar = new SqlSugarScope(configConnection, db => { db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() { //判断是否开启redis设置二级缓存方式 //DataInfoCacheService = new SqlSugarRedisCache(), //模型定义为 int?号时自动为可空 EntityService = (c, p) => { /***高版C#写法***/ //支持string?和string if (p.IsPrimarykey == false && new NullabilityInfoContext() .Create(c).WriteState is NullabilityState.Nullable) { p.IsNullable = true; } } }; db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() { //所有 增、删 、改 会自动调用.RemoveDataCache()清理二级缓存 IsAutoRemoveDataCache = true }; db.Aop.OnError = (exp) => { $"数据库执行错误了:{exp}".LogInformation(); // NlogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "SqlSugar", "执行SQL错误事件", exp); }; //单例参数配置,所有上下文生效 //db.Aop.OnLogExecuting = (sql, pars) => //{ // //获取作IOC作用域对象 // // var appServive = s.GetService(); // // var obj = appServive?.HttpContext?.RequestServices.GetService(); // // Console.WriteLine("AOP" + obj.GetHashCode()); //}; }); //sqlSugar.DbMaintenance.CreateDatabase(); //Type[]? types = DbCoreUntil.DbCodeFirstModes("EzCoreNetFurion.Model", thisAttribute: typeof(CoderFirstAttribute)); //sqlSugar.CodeFirst.InitTables(types ?? new Type[] { }); return sqlSugar; }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { } } }