【发布时间】:2017-10-05 09:48:29
【问题描述】:
如何访问模型中的 connectionString?我正在使用 Dapper。我发现的大多数解决方案都是使用 EntityFramework 而不是 Dapper。
这适用于我的本地机器(macOS):
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
Dapper.DapperHelper.ConnectionString = Configuration["ConnectionStrings:MyConnectionString"];
}
但是它不适用于 Linux Debian。
在我运行应用程序后,Dapper.DapperHelper.ConnectionString 属性在 linux 上为空。
我的 appsettings.json 文件内容:
{
"ConnectionStrings": {
"MyConnectionString": "Server=ip; Database=db_name; User Id=my_user; Password=my_password; Pooling=false;" // SQL Server Authentication
},
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Warning"
}
},
"Console": {
"LogLevel": {
"Default": "Warning"
}
}
}
}
困扰我的是,相同的代码在 macOS 上可以工作,但在 Debian (linux) 上却不工作。
【问题讨论】:
-
1.不要使用静态,这比首先拥有一流 DI 的想法 2. 确保您还复制了您的 appsettings.json
-
我试过非静态的,结果是一样的。也是的,我复制了我的 appsetting.json(整个“发布”目录)。
-
你的 appsettings.json 里面真的有这些部分吗?它通常使用用户机密(它使用项目文件夹之外的 json 文件,因此不会意外提交给版本控制系统)
-
我已将我的 appsettings.json 文件内容添加到我的问题中。是的,我在我的 linux 机器上打开了 appsettings.json 文件,这就是全部。
-
所以你正在搜索“MyConnectionString”但你有一个“RutarConnectionString”?
标签: sql-server-2008 asp.net-core debian connection-string dapper