Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to add migartion to my datavase i keep having this error.Please i am a rookie to MVC

Error Message
ERROR
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<TContext> object in its constructor and passes it to the base constructor for DbContext.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

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.

What I have tried:

.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<Worker> 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<ProdDBContext>(
           options => options.UseSqlServer("Server=DESKTOP-1M2N4KR\\HQ;Database=Rocky;Integrated Security=true;Trusted_Connection=True;TrustServerCertificate=True;"));
        }
    }

}
Posted
Updated 24-Apr-24 7:16am
v2

1 solution

This has nothing to do with migrations at all.

The exception is telling you that you haven't configured your DbContext implementation with a database engine to use. Take a look at your code:
C#
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);
}

Your code is checking to see if the options have already been configured. Hint: the options have NOT been configured! That's why you're getting the exception you're getting. Invert the conditional expression in the if statement and it should work:
C#
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);
}

Just a heads up. You could have figured this out in about 5 seconds using the debugger and stepping through the OnConfiguring code one line at a time to see what was happening.

I'm going to completely ignore the fact you're hard-coding the connection string in your code. That should be stored in a configuration file and your code should be written to grab the connection string from there.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900