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

Time:11-25

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.

CodePudding user response:

You're looking for a connection called "VMSDatabase" in your code. But you've configured a connection called "AgileOneVMSDatabase" in your appsettings. Change either one of them so to the same and this should solve your issue.

CodePudding user response:

According to your description, I guess the reason why you faced this issue is you used the wrong VMSDBContext's constructor method. If this class's constructor method doesn't contains the DbContextOptions parameter, it will face this error.

I suggest you could change it like below:

public VMSDBContext(DbContextOptions options) : base(options) {}
  • Related