Reputation: 343
I am building an enterprise application and i want for the first time the use run the website to be able to choose the SQL server to connect to, and the Database in that server. after that the application always run on that configurations like normal asp.net core app do.
I know that connection string is stored in appsettings.json
so i was thinking about changing it's value at run-time after choosing this configuration, but after searching a lot i think this is not possible.
What do suggest is there a way to do this ?
Upvotes: 0
Views: 1596
Reputation: 493
Make sure that you have installed following NuGet Packages before connect to the SQL Server Db.
First you have to create connection strings in appsettings.json
like below
"ConnectionStrings": {
"DefaultConnection": "Server=DESKTOP-O57GSNN\\MSSQLSERVERNEW,Database=BookListRazor;Trusted_Connection=True;MultipleActiveResultSets=True"},
Here, DefaultConnection is connection Name you are going to use.Please provide any meaningful name you want and it will be use when configuring.Also provide Server Name from SQL Server Management Studio(see the picture below) and Provide a name for your database (Will be created).
Then create ApplicationDbContext.cs
file and type below code
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Book> Book { get; set; }
}
Book is the Model in my App.
Finally configure it with SQL Server configuration inside ConfigureServices
method in Startup.cs
file.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddRazorPages().AddRazorRuntimeCompilation();
}
DefaultConnection is connection Name which was already mentioned in
appsettings.json
Upvotes: 0
Reputation: 11554
Set reloadOnChange
to true when you are adding appsettings.json
to ConfigurationBuilder
:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
then create a class for connection string
public class ConnectionString
{
public string Default {get; set;}
}
And in ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.Configure<ConnectionString>(Configuration.GetSection("ConnectionStrings"));
}
Now this configuration is available through dependency injection.
public class HomeController : Controller
{
private readonly ConnectionString _connectionString;
public HomeController(IOptionsSnapshot<ConnectionString> connectionString)
{
_connectionString = connectionString.Value;
}
}
IOptionsSnapshot
can reload configuration data when it changes.
More info about Configuration.
Upvotes: 1
Reputation: 75
You must to load the appsettings.json file in Program.cs
public static void Main(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.Build();
string conStr = configuration.GetConnectionString("DefaultConnection");
// your code
var host = WebHost.CreateDefaultBuilder();
host.Run();
}
Upvotes: 0