question

VikashKumarPrasad-2674 avatar image
0 Votes"
VikashKumarPrasad-2674 asked ZhiLv-MSFT commented

Some services are not able to be constructed dbcontext

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.

dotnet-aspnet-core-generaldotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered ZhiLv-MSFT commented

Hi @VikashKumarPrasad-2674,

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





image.png (36.0 KiB)
26.gif (401.9 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ZhiLv-MSFT : I am not sure, Whether you read my questions properly or not. The given answer is how to use entity framework core in .net core. That i know, How to implement. But here is some specific scenario which i discussed in question and i need to achieve this. Kindly re-read question once again and provide the solution.

0 Votes 0 ·
ZhiLv-MSFT avatar image ZhiLv-MSFT VikashKumarPrasad-2674 ·

Hi @VikashKumarPrasad-2674,

I have updated the reply, please refer it, and try to use the AddSingleton() method to register the service. If have any question about my reply, please let me know freely.

0 Votes 0 ·
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 edited

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

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.