Not able to configure connection string in ASP.net MVC Core 5.0 & Entity Framework Core application

Mohan Raju 40 Reputation points
2021-11-27T08:37:33.433+00:00

I'm getting below error when I configure SQL Connection in ASP.Net Core MVC 5 and Entity Framework core.

I've configured in Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();

        services.AddDbContext<VMSDBContext>(
           options => options.UseSqlServer(Configuration.GetConnectionString("VMSDatabase"))
        );
    } 

My appsettings.json file:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"VMSDatabase": "Server=.;Database=VMS;Trusted_Connection=True;"
},
"AllowedHosts": "*"
}
InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2021-11-29T18:14:44.107+00:00

    Your design has a parameterless constructor. You get the error "InvalidOperationException: No database provider has been configured for this DbContext." if you call the parameterless constructor in an Action or Razor Page.

     _context = new VMSDBContext();
    

    Remove the parameterless constructor and always use standard constructor injection which has the following pattern.

        public class IndexModel : PageModel
        {
            private readonly SchoolContext _context;
    
            public IndexModel(SchoolContext context)
            {
                _context = context;
            }
        }
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2021-11-29T03:28:26.48+00:00

    Hi anonymous user,

    Can you post the related code about the VMSDBContext?

    From the error message, the issue might relate that your DbContext(VMSDBContext) needs a constructor which accepts a DbContextOptions. You can refer the following sample code which works well on my side:

    public class ApplicationDbContext : IdentityDbContext  
    {  
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
            : base(options)  
        {  
        }  
    }  
    

    Appsettings.json

    {  
      "ConnectionStrings": {  
        "DefaultConnection": "...connection string..."  
      },  
    

    Code in the ConfigureService method:

            services.AddDbContext<ApplicationDbContext>(options =>  
                options.UseSqlServer(  
                    Configuration.GetConnectionString("DefaultConnection")));   
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.