Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core - No database provider has been configured for this DbContext

I am new to EF Core 1.0. Getting below error when I run below command during DB migration

Command

 migrations add ApplicationUserIsActive -c ApplicationDbContext

Error:

System.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.

The StartUp.cs

public class Startup
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddEntityFrameworkSqlServer()
            .AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=employee;Trusted_Connection=True;MultipleActiveResultSets=true;")
       .UseInternalServiceProvider(serviceProvider));

        services.AddEntityFrameworkSqlServer()
            .AddDbContext<EmplooyeeDbContext>((serviceProvider, options) =>
options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=employee;Trusted_Connection=True;MultipleActiveResultSets=true;")
       .UseInternalServiceProvider(serviceProvider));

        services.AddTransient<IUserContext, SeedUserContext>();
    }

}

.csproj file

 <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.0.0">
  <PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.DataAnnotations" Version="1.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />

However when I execute the migration command for other DBContext i.e. "EmplooyeeDbContext" the migration commend works fine.

How can I resolve this issue?

like image 405
Arayn Avatar asked Dec 19 '22 06:12

Arayn


1 Answers

I figured out the issue. It was due to the default constructor of "ApplicationDbContext" which was causing the issue. After removing the default constructor for "ApplicationDbContext" it all started working fine.

Thanks for all your support.

like image 137
Arayn Avatar answered Apr 19 '23 14:04

Arayn