0

The situation

I'm working on a school project and I use Blazor (Server-Side) for the Frontend. I do have a business layer and a DataAcces Layer (both .Net Core Class Libraries). I'm using Code First to create an SQLite database.

The problem/error

I somehow get following messages when trying to execute Add-Migration InitialCreate:

The warning

An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: 'AddDbContext' was called with configuration, but the context type 'TemperBapesContext' only declares a parameterless constructor. This means that the configuration passed to 'AddDbContext' will never be used. If configuration is passed to 'AddDbContext', then 'TemperBapesContext' should declare a constructor that accepts a DbContextOptions and must pass it to the base constructor for DbContext.

The error

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.

Project Architecture and Description

The Startup project is my Blazor application.

project architecture

My appsettings.json contains the ConnectionString to the database:

  "ConnectionStrings": {
    "SQLite": "Data Source=L:\\M183\\m183\\project\\SQLite\\TemperBapesDB.db;"
  }

My Startup.cs ConfigureServices() looks like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
    services.AddServerSideBlazor();
    services.AddBussinesLayer(Configuration);
}

The SetupExtensions.cs on Business Level looks like this:

public static void AddBussinesLayer(this IServiceCollection services, IConfiguration configuration)
{
    services.AddDataAccess(configuration);
}

The SetupExtensions.cs on DataAccess Level looks like this:

public static void AddDataAccess(this IServiceCollection services, IConfiguration configuration)
{
    services.AddDbContext<TemperBapesContext>(options => options.UseSqlite(configuration.GetConnectionString("SQLite")));
}

NuGet-Packages Installed

Blazor project:

  • Microsoft.EntityFrameworkCore.Design

Business Layer:

  • Microsoft.Extensions.Configuration.Abstractions
  • Microsoft.Extensions.DependencyInjection.Abstractions

DataAccess Layer:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Sqlite
  • Microsoft.EntityFrameworkCore.Tools

1 Answers1

1
public class TemperBapesContext : DbContext
{
    // Your DbSets 

    // This constructor is needed
    public TemperBapesContext(DbContextOptions<TemperBapesContext> options)
        : base(options)
    {
    }
}

You need to add a constructor where DbContextOptions can be injected

Cem.S
  • 1,001
  • 10
  • 15
  • Omg I feel so embarrassed right now. I've searched for a solution everywhere but there. Thanks a lot! Already worked with this kind of projects I just forgot a lot of things again. – Eldar Omerovic Nov 27 '20 at 17:01