Hi @Vikash Kumar Prasad ,
Based on your code (the MyContext class), when you create the MyContext instance you need call the constructor method and set the IAppSetting, you can refer the following sample:
In the following dbContext, I will set the connection string.
public class ApplicationDbContext : DbContext
{
private readonly string _connectionString;
public ApplicationDbContext(string connectionString)
{
_connectionString = connectionString;
}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString);
}
//tables
}
Then, in the ConfigureService method, register the service using AddSingleton()
method, instead of the AddDbContext()
method.
services.AddSingleton<ApplicationDbContext>(s=> new ApplicationDbContext(Configuration.GetConnectionString("DefaultConnection")));
The result as below:
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion