I'm really new in this API development, so I have the next question.
Is it possible to get the DB name at run time for my connection string? This because the user will be sending the DB name through a Json file. I'm working with Visual Studio 2019, NetCore 3.1.
I have my appsettings.json like this:
"ConnectionStrings": {
"ImportConnection": "Data Source=SQLSERVER;Initial Catalog={dbName};User ID=Id;Password=Pass;MultipleActiveResultSets=True"
},
And my Startup.cs in configureservices I have this
services.AddDbContext<ApplicationDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ImportConnection")));
The user send a Json file with the next structure
{
"ImportTypeId": 1,
"Data": [
{
"LayoutID": 6,
"ClientID": "XXX",
Where ClientID is the DB name.
I really want to know if there's a way to replace the Initial Catalog {dbName} in my connectionString with the ClientID value.
Also this is my DbContext
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace API_Test.Models
{
public class ApplicationDBContext : IdentityDbContext<ApplicationUser>//DbContext
{
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
{
}
public DbSet<DataModel> API_DATA { get; set; }
}
}
Thank you so much