我正在寻找最佳实践方法在.net Core 2 MVC应用程序中的appsettings.json中存储连接字符串(就像在MVC 5中的web.config中一样)
我想使用Dapper而不是EF(我发现了许多EF示例)。
这样的事情:
{
"ConnectionStrings": {
"myDatabase": "Server=.;Database=myDatabase;Trusted_Connection=true;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}
当然网上有很多例子吗?我无法找到.net core 2.0。
在1到2之间发生了一些变化,我想确保我使用的是第2版最佳做法。
我发现了这个 - 但似乎是.net核心1: Visual Studio 2017 - MVC Core - Part 05 - Connection String from appsettings.json
这使用键值对appsettings - 而不是连接字符串: Read AppSettings in ASP.NET Core 2.0
同样不清楚这是.net Core 1还是2:Net Core Connection String Dapper visual studio 2017
答案 0 :(得分:8)
appsettings.json
{
"connectionStrings": {
"appDbConnection: : "..."
}
}
如果您遵循约定并在connectionStrings
下定义连接字符串,则可以使用扩展方法GetConnectionString()
来读取其值。
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"))
);
// ...
}
}
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 });
}
}
您可以使用Options Pattern。
定义一个与appsettings.json中的JSON
对象结构完全匹配的类
public class ConnectionStringConfig
{
public string AppDbConnection { get; set; }
}
在Startup上注册该配置
public void ConfigureServices(IServiceCollection services)
{
// ...
services.Configure<ConnectionStringConfig>(
this.Configuration.GetSection("connectionStrings")
);
// ...
}
接收POCO中的访问者
public class YourPoco
{
private readonly ConnectionStringConfig _connectionStringConfig;
public YourPoco(IOptions<ConnectionStringConfig> configAccessor)
{
_connectionStringConfig = configAccessor.Value;
// Your connection string value is here:
// _connectionStringConfig.AppDbConnection;
}
}
注意:
答案 1 :(得分:2)
如下图所示,放在appsettings.json中。
"ConnectionStrings": {
"DefaultConnection": "Data Source=;Initial Catalog=;Persist Security Info=True;User ID=; Password=;"
}
在Startup.cs中获取它,如下所述:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
}
使用依赖注入在控制器中注入配置,如下所述:
public class MyController : Controller
{
private readonly IConfiguration _configuration;
private string connectionString;
public MyController(IConfiguration configuration)
{
_configuration = configuration;
connectionString = _configuration.GetConnectionString("DefaultConnection");
}
}