Search code examples
c#entity-frameworkmigration.net-6.0

No database provider has been configured for this DbContext in .net 6 preview 7


I'm using .net 6, and when I tried to do a migration, I had a problem:

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.

I read another 6 question in Stack, but although the error message is the same not the solutions or the problems seems to be the same, I think the problem is related to .net 6 preview 7, help me find a solution.

I Have 3 projects

Jupyter.Core.Api -> Has DbConfiguration

Jupyter.Core.Data -> Has DbContext

Jupyter.Core -> Has 1 Model

And

Jupyter.Core.Api references Jupyter.Core.Data

Jupyter.Core.Data references Jupyter.Core

In Jupyter.Core.Api I have the following PackageReference relate to the problem:

<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.9">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql" Version="5.0.7" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />

In Jupyter.Core.Data I have the following PackageReference relate to the problem:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.7.21378.4" />

In the Jupyter.Core.Api inside Program.cs my code is like this, there is no Startup.cs in .net 6 template

using Jupyter.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

builder.Services.AddDbContext<StationContext>(options => options.UseNpgsql(builder.Configuration.GetConnectionString("StationConnection")));

builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "Juptyer.Core.Api", Version = "v1" });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Juptyer.Core.Api v1"));
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

And in Jupyter.Core.Data the StationContext.cs is like this.

using System;
using Jupyter.Core.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace Jupyter.Core.Data;

public partial class StationContext : DbContext
{
    public StationContext() { }

    public StationContext(DbContextOptions<StationContext> options) : base(options) { }

    protected override void OnConfiguring(DbContextOptionsBuilder opitionsBuilder)
    {
        base.OnConfiguring(opitionsBuilder);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasAnnotation("Relational:Collation", "en_US.utf8");

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);

    public DbSet<User> Users { get; set; }
}

I using Visual Studio, I trying the fallowing command in package console.

add-migration 'User' -Context StationContext

My default startup project is set to Jupyter.Core.Api and inside the package console is set to Jupyter.Core.Data.

The message talk about, No database provider has been configured for this DbContext, but I did it in Program.cs

builder.Services.AddDbContext<StationContext>(options => 
    options.UseNpgsql(builder.Configuration.GetConnectionString("StationConnection")));

It also talks about "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." and I also did that.

protected override void OnConfiguring(DbContextOptionsBuilder opitionsBuilder)
{
    base.OnConfiguring(opitionsBuilder);
}

I also know that DbContext class should have a contructor like this.

public StationContext(DbContextOptions<StationContext> options) : base(options) { }

So, what I'm missing? any hint?


Solution

  • Well, I was trying to find a solution for few hours. After I posted this question, I was searching for a solution for like 40 minutes. And then I find out why it was not working.

    Everything in the code is right except the PackageReference.

    In Jupyter.Core.Api instead of this

    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.9">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Npgsql" Version="5.0.7" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />
    

    It should be like this

    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.7.21378.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-preview7" />
    

    And in Jupyter.Core.Data instead of this

    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.7.21378.4" />
    

    It should be like this

    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0-preview.7.21378.4" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-preview7" />
    

    That solved my problem.