引入mysql包。修改配置

This commit is contained in:
falcon 2020-04-22 20:59:52 +08:00
parent dad6ae3362
commit d4ed14d313
4 changed files with 18 additions and 4 deletions

View File

@ -9,7 +9,7 @@ namespace FAuth.DataBase
public class FAuthDb:DbContext public class FAuthDb:DbContext
{ {
public FAuthDb(DbContextOptions options) : base(options) { public FAuthDb(DbContextOptions options) : base(options) {
Database.EnsureCreatedAsync().Wait(); //Database.EnsureCreatedAsync().Wait();
} }
protected override void OnModelCreating(ModelBuilder modelBuilder) { protected override void OnModelCreating(ModelBuilder modelBuilder) {

View File

@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" /> <PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.3" /> <PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="3.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.3" />
<PackageReference Include="MySql.Data" Version="8.0.19" />
<PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.19" /> <PackageReference Include="MySql.Data.EntityFrameworkCore" Version="8.0.19" />
<PackageReference Include="NLog" Version="4.7.0" /> <PackageReference Include="NLog" Version="4.7.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.2" /> <PackageReference Include="NLog.Web.AspNetCore" Version="4.9.2" />

View File

@ -39,7 +39,18 @@ namespace FAuth
services.AddMsJsonProvider(); services.AddMsJsonProvider();
//注册数据库 //注册数据库
services.AddDbContext<FAuthDb>(option => { services.AddDbContext<FAuthDb>(option => {
option.UseSqlServer(Configuration.GetConnectionString("FAuthDb")); //option.UseSqlServer(this.Configuration.GetValue<string>("Database:FAuthDbSqlServer"));
var dbType = this.Configuration.GetValue<string>("Database:UseDb").ToLower();
switch(dbType) {
case "mysql":
option.UseMySQL(this.Configuration.GetValue<string>("Database:FAuthDbMySql"));
break;
case "sqlserver":
option.UseSqlServer(this.Configuration.GetValue<string>("Database:FAuthDbSqlServer"));
break;
default:
throw new Exception("Database:UseDbÅäÖôíÎó£¡Ö»ÄÜΪmysql»òsqlserver");
}
}); });
services.AddAccountHelper(); services.AddAccountHelper();
//注册Redis //注册Redis

View File

@ -7,8 +7,10 @@
} }
}, },
"AllowedHosts": "*", "AllowedHosts": "*",
"ConnectionStrings": { "Database": {
"FAuthDb": "Server=.\\SQLSERVER2008R2;Database=FAuth;User ID=sa;Password=111" "UseDb": "sqlserver",
"FAuthDbSqlServer": "Server=.\\SQLSERVER2008R2;Database=FAuth;User ID=sa;Password=111",
"FAuthDbMySql": "server=localhost;user id=root;password=root;persistsecurityinfo=True;database=FAuth;SslMode=none"
}, },
"Redis": { "Redis": {
"InstanceName": "", "InstanceName": "",