在 ASP.net Core 2 MVC 应用程序中从 appSettings.json 存储/检索 ConnectionString [英] Store / Retrieve ConnectionString from appSettings.json in ASP.net Core 2 MVC app
问题描述
我正在寻找将连接字符串存储在 .net Core 2 MVC 应用程序中的 appsettings.json 中的最佳实践方法(就像您在 MVC 5 中的 web.config 中所做的那样).
I'm looking for the best practice way to store a connection string in appsettings.json in a .net Core 2 MVC app (like you do in web.config in MVC 5).
我想使用 Dapper 而不是 EF(我发现了很多 EF 示例).
I want to use Dapper not EF (I found many EF examples).
像这样:
{
"ConnectionStrings": {
"myDatabase": "Server=.;Database=myDatabase;Trusted_Connection=true;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
网上肯定有很多例子吗?我找不到任何适用于 .net core 2.0 的东西.
Surely there are many examples online? Nothing I can find that is for .net core 2.0.
在 1 和 2 之间发生了一些变化,我想确保我使用的是版本 2 的最佳实践.
Several things have changed between 1 and 2 and I want to ensure I'm using version 2 best practices.
我发现了这个 - 但它似乎是 .net core 1:Visual Studio 2017 - MVC 核心 - 第 05 部分 - 来自 appsettings.json 的连接字符串
I've found this - but it seems to be .net core 1: Visual Studio 2017 - MVC Core - Part 05 - Connection String from appsettings.json
这使用键值对 appsettings - 而不是连接字符串:在 ASP.NET Core 2.0 中读取 AppSettings
This uses key value pair appsettings - not the connectionstrings: Read AppSettings in ASP.NET Core 2.0
同样不清楚这是 .net Core 1 还是 2:Net Core Connection String Dapper Visual Studio 2017
Again it's unclear if this is .net Core 1 or 2: Net Core Connection String Dapper visual studio 2017
推荐答案
在 appsettings.json
中定义您的连接字符串
Define your connection string(s) in appsettings.json
{
"connectionStrings": {
"appDbConnection": "..."
}
}
在启动时读取其值
如果您遵循约定并在connectionStrings
下定义连接字符串,则可以使用扩展方法GetConnectionString()
读取其值.
Read its value on Startup
If you follow the convention and define your connection string(s) under connectionStrings
, you can use the extension method GetConnectionString()
to read its value.
public class Startup
{
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
// Since you said you're using Dapper, I guess you might want to
// inject IDbConnection?
services.AddTransient<IDbConnection>((sp) =>
new SqlConnection(this.Configuration.GetConnectionString("appDbConnection"))
);
// ...
}
}
在存储库中使用 IDbConnection?
public interface ISpecificationRepository
{
Specification GetById(int specificationId);
}
public SpecificationRepository : ISpecificationRepository
{
private readonly IDbConnection _dbConnection;
public SpecificationRepository(IDbConnection dbConnection)
{
_dbConnection = dbConnection;
}
public Specification GetById(int specificationId)
{
const string sql = @"SELECT * FROM [YOUR_TABLE]
WHERE Id = @specId;";
return _dbConnection
.QuerySingleOrDefault<Specification>(sql,
new { specId = specificationId });
}
}
只需要 POCO 中的连接字符串?
您可以使用选项模式.
定义一个与 appsettings.json 中的
JSON
对象结构完全匹配的类
Define a class that exactly matches the
JSON
object structure in appsettings.json
public class ConnectionStringConfig
{
public string AppDbConnection { get; set; }
}
在启动时注册该配置
Register that configuration on Startup
public void ConfigureServices(IServiceCollection services)
{
// ...
services.Configure<ConnectionStringConfig>(
this.Configuration.GetSection("connectionStrings")
);
// ...
}
在您的 POCO 中接收访问器
Receive the accessor in your POCO
public class YourPoco
{
private readonly ConnectionStringConfig _connectionStringConfig;
public YourPoco(IOptions<ConnectionStringConfig> configAccessor)
{
_connectionStringConfig = configAccessor.Value;
// Your connection string value is here:
// _connectionStringConfig.AppDbConnection;
}
}
注意事项:
- See my sample codes on how to read values from appsettings.json both on Core 1.x and 2.0.
- See how I setup if you have more than 1 connection string.
这篇关于在 ASP.net Core 2 MVC 应用程序中从 appSettings.json 存储/检索 ConnectionString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!