Some services are not able to be constructed dbcontext

Vikash Kumar Prasad 1 Reputation point
2021-08-25T10:56:57.35+00:00

Hi All,

I am migrating one asp.net core 3.1 from asp.net project. Here i have one dbcontext which connection string i need to pass from one of my appservice class. To use appservice, in this dbcontext i used something like below:

public class MyContext: DbContext
{
  private readonly IAppSetting _appSetting;

  public MyContext(IAppSetting appSetting) 
  {
      _appSetting = appSetting;
  }
  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
              optionsBuilder.UseSqlServer(_appSetting.Myconnectionstring, opts=> opts.CommandTimeout((int)TimeSpan.FromMinutes(1).TotalSeconds));
  }
}

In StartUp.cs file i have configure this context service like below:

public void ConfigureServices(IServiceCollection services)
{
  services.AddDbContext<MyContext>();
}

But when i run the application it is throwing runtime error like :

Some services are not able to be constructed dbcontext.

Need help.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,189 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2021-08-26T02:39:26.587+00:00

    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:

    126948-26.gif


    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


  2. Duane Arnold 3,216 Reputation points
    2021-08-26T23:32:09.37+00:00

    Maybe you should look into various ways to configure DBcontext in using dependency injection of the connection string.

    https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/

    0 comments No comments