要使用MySQL而不是SQL Server,您需要在项目中包含MySQL providers for Entity Framework Core之一。在Visual Studio 2015中,这意味着修改您的project.json文件以包含依赖项。您添加的依赖关系取决于您选择使用哪个提供程序。例如,如果您选择使用SapientGuardian,你会增加SapientGuardian.EntityFrameworkCore.MySql
您project.json的dependencies
部分,像这样:
"dependencies": {
"SapientGuardian.EntityFrameworkCore.MySql": "7.1.19"
}
用于ASP.NET中,标准的约定是做配置在Startup
类,但你也可以像在发布的示例中那样在您的DbContext
中配置数据库连接字符串。要在Startup
类配置(假设你的连接字符串存储在一个名为connectionString
一个字符串变量,修改该行,您添加DbContext
到这一点:
app.AddDbContext<HunterViewsContext>(options => options.UseMySQL(connectionString));
通过这样做,你将不再需要重写该OnConfiguring
方法在DbContext
。但是,如果你想在DbContext
使用OnConfiguring
相反,你可以简单地调用optionsBuilder.UseMySQL(connectionString)
有代替。
现在,这里就是它变得有点棘手。由于撰写了MySQL的服务使用自SIG ned SSL证书作为代表VCAP_SERVICES环境变量中的PEM格式证书的Base64编码字符串提供,您需要将该证书转换为PFX格式,然后才能将其用于MySQL提供程序。
您有两个选择来完成此转换。第一个选择是使用一些外部工具将证书转换为PFX格式,并将该文件与您的应用程序一起推送。另一种解决方案是在配置数据库连接时,在应用程序启动时即时转换证书。
可以使用BouncyCastle的NuGet包做对飞的转换,像这样:
private static void CreatePfxFromPemCertificate(string base64encodedPem, string pfxFilePath, string pfxPassword)
{
// get the PEM certificate, then convert to pfx format
byte[] bytes = Convert.FromBase64String(base64encodedPem);
Pkcs12Store store = new Pkcs12StoreBuilder().Build();
X509CertificateEntry[] chain = new X509CertificateEntry[1];
object pemObject;
using (var streamReader = new StreamReader(new MemoryStream(bytes)))
{
PemReader pemReader = new PemReader(streamReader);
if ((pemObject = pemReader.ReadObject()) is X509Certificate)
{
chain[0] = new X509CertificateEntry((X509Certificate)pemObject);
}
}
store.SetCertificateEntry(pfxFilePath, chain[0]);
var certFile = File.Create(pfxFilePath);
store.Save(certFile, pfxPassword.ToCharArray(), new SecureRandom());
certFile.Flush();
certFile.Dispose();
}
此功能将要求这些using语句:
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
而且这种依赖项目。JSON:
"Portable.BouncyCastle-Signed": "1.7.0.2",
该函数接受3个参数:
- Base64编码串(在VCAP_SERVICES环境变量提供)
- 到将要写入
- 的PFX文件的路径应该用来保护pfx文件的密码
既然我们有一个函数c转换Base64编码的证书,是时候把它放在一起,并从Startup
类中的VCAP_SERVICES
环境变量创建连接字符串。
首先,在Startup
类的构造函数:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("vcap-local.json", optional:true); // when running locally, store VCAP_SERVICES credentials in vcap-local.json
Configuration = builder.Build();
// try to get the VCAP_SERVICES environment variable (when running on Bluemix)
string vcapServices = Environment.GetEnvironmentVariable("VCAP_SERVICES");
if (vcapServices != null)
{
JObject json = JObject.Parse(vcapServices);
var credentialsToken = json.SelectToken("compose-for-mysql")? // look for compose-for-mysql instance
.FirstOrDefault()? // get first database instance
.SelectToken("credentials"); // get the credentials
// get the uri
Configuration["compose-for-mysql:0:credentials:uri"] = credentialsToken?.SelectToken("uri");
// get the base64 certificate
Configuration["compose-for-mysql:0:credentials:ca_certificate_base64"] = credentialsToken?.SelectToken("ca_certificate_base64");
}
}
此代码将从VCAP_SERVICES
环境变量抢数据库开放的,或在您的项目目录名为vcap-local.json
如果你正在本地运行一个JSON文件(您可以从Bluemix UI中的连接选项卡复制凭证)。
为了把这个都聚集在你的ConfigureServices
方法来创建数据库字符串中的Startup
类:
public void ConfigureServices(IServiceCollection services)
{
var databaseUri = Configuration["compose-for-mysql:0:credentials:uri"];
var username = (databaseUri.Split('/')[2]).Split(':')[0];
var password = (databaseUri.Split(':')[2]).Split('@')[0];
var port = (databaseUri.Split(':')[3]).Split('/')[0];
var hostname = (databaseUri.Split('@')[1]).Split(':')[0];
var database = databaseUri.Split('/')[3];
// create the connection string
var connectionString = $"Server={hostname};Port={port};uid={username};pwd={password};Database={database};SSL Mode=Required;";
// convert the Base64 encoded PEM SSL certificate to PFX format
CreatePfxFromPemCertificate(config[$"compose-for-mysql:0:credentials:ca_certificate_base64"],
"compose-for-mysql0.pfx", password);
// add the ssl certificate to the connection string
connectionString += "CertificateFile=compose-for-mysql0.pfx;";
connectionString += $"CertificatePassword={password};";
// add database context
services.AddDbContext<HunterViewsContext>(options => options.UseMySQL(connectionString));
// Add framework services.
services.AddMvc();
}
请使用BouncyCastle的,Portable.BouncyCastle官方便携版。我是便携式叉子的维护者。如果遇到问题,请在回购协议中打开一个问题:https://github.com/onovotny/bc-csharp –