How can i access the connectionString inside my model? I'm using Dapper. Most of the solutions i found were using EntityFramework and not Dapper.
This is working on my local machine (macOS):
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
Dapper.DapperHelper.ConnectionString = Configuration["ConnectionStrings:MyConnectionString"];
}
But it doesn't work on Linux Debian.
The Dapper.DapperHelper.ConnectionString
property is null on linux after i run the application.
My appsettings.json file contents:
{
"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"
}
}
}
}
What bothers me is, that the same code works on macOS but doesn't work on Debian (linux).
In my Program.cs file I had to replace this:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseUrls("http://localhost:8888")
.Build();
}
with this:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.UseStartup<Startup>()
.UseUrls("http://localhost:8888")
.Build();
}
which is the same as:
public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.UseStartup<Startup>()
.UseUrls("http://localhost:8888")
.Build();
}
List of using declarations:
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
Then it worked on Debian Linux as well as macOS.
This helped me figure it out: https://joonasw.net/view/aspnet-core-2-configuration-changes
But this part:
WebHost.CreateDefaultBuilder ASP.NET Core 2.0 offers a nice convenience method for creating a very typical configuration for an application.
Instead of writing this:
...
You can just write:
...
is not true for Debian Linux. This works only on macOS. Haven't tested it on Windows.
I also reported this as a bug: https://github.com/dotnet/corefx/issues/24446