heisenberg
heisenberg

Reputation: 1312

Unable to create migrations on EF Core 6

I am having this issue when running dotnet ef migrations add Initial and I have no idea what is causing it. The issue is I am getting the following error:

Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

I have been going over and over the code and I can't find the problem.

This is my Program.cs code:

using Microsoft.EntityFrameworkCore;
using TrainningManagerService.Context;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
});

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

And this is my ApplicationDbContext:

using Microsoft.EntityFrameworkCore;
using TrainningManagerService.Entities.Database;

namespace TrainningManagerService.Context
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :
            base(options)
        {
        }

        public DbSet<Video> Videos { get; set; }
        public DbSet<Plan> Plans { get; set; }
    }
}

I have been going all over the internet and I can't find a solution anywhere, and this is really bothering me because I've just created a new solution and I just can't create one migration.

Upvotes: 2

Views: 2479

Answers (1)

heisenberg
heisenberg

Reputation: 1312

This was being caused by the order in Program.cs.

I had to move up this part:

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
});

So it became:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
});

var app = builder.Build();

Otherwise the builder was being done before teh database was applied

Upvotes: 2

Related Questions