I am trying to add migartion to my database i keep having this error.Please i am a rookie to MVC.My code
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace DemoRepository.Model
{
public class ProdDBContext : DbContext
{
public DbSet
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Server=DESKTOP-1M2N4KR\\EMMA;Database=Rocky;Integrated Security=true; Trusted_Connection=True;TrustServerCertificate=True;");
}
base.OnConfiguring(optionsBuilder);
}
public void ConfigureServices(IServiceCollection services)
{
IServiceCollection serviceCollection = services.AddDbContext
options => options.UseSqlServer("Server=DESKTOP-1M2N4KR\\HQ;Database=Rocky;Integrated Security=true;Trusted_Connection=True;TrustServerCertificate=True;"));
}
}
}
Error Message
Unable to create a 'DbContext' of type ''. The exception '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
My problem basically is that i can not add migration to the database. I keep getting error message as posted above.I have installed Entityframework core,Entityframework,sql and lastly the Entityframework.Tools.Please help me.

Jobin SRank #886 · 2.4K reputationPosted Apr 24, 2024, 9:10 PM EDT
Hi emma oj,
Constructor section missing
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace DemoRepository.Model { public class ProdDBContext : DbContext { public ProdDBContext(DbContextOptions options):: base(options){ } public DbSet Workers { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer("Server=DESKTOP-1M2N4KR\\EMMA;Database=Rocky;Integrated Security=true; Trusted_Connection=True;TrustServerCertificate=True;"); } base.OnConfiguring(optionsBuilder); } public void ConfigureServices(IServiceCollection services) { IServiceCollection serviceCollection = services.AddDbContext( options => options.UseSqlServer("Server=DESKTOP-1M2N4KR\\HQ;Database=Rocky;Integrated Security=true;Trusted_Connection=True;TrustServerCertificate=True;")); } } }Jayraj ChhayaRank #304 · 8.2K reputationPosted Apr 25, 2024, 1:58 AM EDT
To resolve the migration issue, follow these steps:
Move the
ConfigureServicesmethod to theStartup.csfile in your MVC project. This method should be used to configure services for dependency injection, not within theDbContextclass.Ensure that the
DbContextclass only contains the necessary configurations related to the database context itself, such as entity mappings and database connection settings.Modify the
Startup.csfile to include theAddDbContextmethod for configuring theDbContextand database provider. Here is an example of how it should look:public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); }OnConfiguringmethod in theProdDBContextclass to remove the hard-coded connection string and rely on the configuration from theStartup.csfile. This ensures that the database connection is configured correctly.Jaimin ShethiyaRank #177 · 13.7K reputationPosted Apr 25, 2024, 1:05 AM EDT
Hello emma oj,
Can you plese replace the your code with below one and after try it.
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace DemoRepository.Model { public class ProdDBContext : DbContext { public ProdDBContext(DbContextOptions options) : base(options) { } public DbSet Workers { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { if (optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer("Server=DESKTOP-1M2N4KR\\EMMA;Database=Rocky;Integrated Security=true; Trusted_Connection=True;TrustServerCertificate=True;"); } base.OnConfiguring(optionsBuilder); } public void ConfigureServices(IServiceCollection services) { IServiceCollection serviceCollection = services.AddDbContext( options => options.UseSqlServer("Server=DESKTOP-1M2N4KR\\HQ;Database=Rocky;Integrated Security=true;Trusted_Connection=True;TrustServerCertificate=True;")); } } }Thanks