最近公司不少业务都要求上云,云上的数据库购买了mysql(估计是应为便宜吧),因此我用abp框架开发的应用都要逐步切换到mysql。通过一阵摸索踩坑,总算是切换成功了,因此先记录下方便后续切换使用,也顺便分享给有须要的朋友。mysql
其实集成mysql主要是参照官方文档进行就能够了(官方文档:https://aspnetboilerplate.com/Pages/Documents/EF-Core-MySql-Integration),这里也是简单描述下关键步骤:sql
安装Pomelo.EntityFrameworkCore.MySqlNuGet包到*.EntityFrameworkCore 项目上数据库
替换YourProjectNameDbContextConfigurer.cs文件,将“UseSqlServer”替换为“UseMySql”,以下
json
1 public static class WeChatDbContextConfigurer 2 { 3 public static void Configure(DbContextOptionsBuilder<WeChatDbContext> builder, string connectionString) 4 { 5 builder.UseMySql(connectionString); 6 } 7 8 public static void Configure(DbContextOptionsBuilder<WeChatDbContext> builder, DbConnection connection) 9 { 10 builder.UseMySql(connection); 11 } 12 }
修改appsettings.json中的数据库连接,如:app
{ "ConnectionStrings": { "Default": "server=127.0.0.1;uid=root;pwd=1234;database=mysqldemodb" }, ... }
修改防止EF Core调用Program.BuildWebHost(),须要将BuildWebHost函数更名,录入改为InitWebHost。以下框架
1 public class Program 2 { 3 public static void Main(string[] args) 4 { 5 InitWebHost(args).Run(); 6 } 7 8 public static IWebHost InitWebHost(string[] args) 9 { 10 return WebHost.CreateDefaultBuilder(args) 11 .UseStartup<Startup>() 12 .Build(); 13 } 14 }
打开程序包管理控制台,选择默认项目*.EntityFrameworkCore,执行命令“update-database”便可。以下 ide
卸载以前NuGet中abp默认安装的hangfire包,并注释Module中默认将hangfire的后台任务的注入代码。(如以前没有使用sqlserver的hangfire,次步骤可忽略)函数
[DependsOn(typeof (AbpHangfireAspNetCoreModule))] public class MyProjectWebModule : AbpModule { public override void PreInitialize() { //注释hangfire //Configuration.BackgroundJobs.UseHangfire(); } //... }
1 public IServiceProvider ConfigureServices(IServiceCollection services) 2 { 3 //........ 4 //配置msql下的hangfire 5 services.AddHangfire(x => x.UseStorage(new MySqlStorage( 6 _appConfiguration.GetConnectionString("Default"), 7 new MySqlStorageOptions 8 { 9 TransactionIsolationLevel = IsolationLevel.ReadCommitted, // 事务隔离级别。默认是读取已提交。 10 QueuePollInterval = TimeSpan.FromSeconds(15), //- 做业队列轮询间隔。默认值为15秒。 11 JobExpirationCheckInterval = TimeSpan.FromHours(1), //- 做业到期检查间隔(管理过时记录)。默认值为1小时。 12 CountersAggregateInterval = TimeSpan.FromMinutes(5), //- 聚合计数器的间隔。默认为5分钟。 13 PrepareSchemaIfNecessary = true, //- 若是设置为true,则建立数据库表。默认是true。 14 DashboardJobListLimit = 50000, //- 仪表板做业列表限制。默认值为50000。 15 TransactionTimeout = TimeSpan.FromMinutes(1), //- 交易超时。默认为1分钟。 16 TablePrefix = "Hangfire" 17 } 18 ))); 19 //........ 20 }
1 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 2 { 3 app.UseHangfireServer(); //启用hangfire服务 4 app.UseHangfireDashboard(); //使用hangfire面板 5 }
本地访问地址:https://localhost:端口/Hangfire sqlserver
将hangfire部署在iis中时,必需要证应用程序一直运行,不会被iis回收。ui
a.设置应用程序池,
b.右键应用程序池-》高级设置,配置以下启动模式为AlwaysRunning,闲置超时设置为0;
c.邮件站点,选择高级设置,设定预加载启用为true。