Aleksej_Shherbak
Aleksej_Shherbak

Reputation: 3068

No migrations were applied. The database is already up to date. Done

I have some set of models:

enter image description here

I've run add migrations command

dotnet ef migrations add <migration name>

And now I have the following migrations set:

enter image description here

When I start dotnet ef database update I see my tables is SQL client and all works nice.

A problem starts when I connect to another database. I would like to apply my migrations to a new database. But when I run dotnet ef database update I see No migrations were applied. The database is already up to date. Done. message. The command creates me only __EFMigrationsHistory table and it's empty. How I can use my migrations on new database?

Here is my ApplicationContext

public class ApplicationContext : DbContext
{
    private readonly string _connectionString;

    public ApplicationContext(IConfiguration configuration)
    {
        _connectionString = configuration.GetConnectionString("Recipes");
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Recipe> Recipes { get; set; }
    public DbSet<RecipeStep> RecipeSteps { get; set; }
    public DbSet<Ingredient> Ingridients { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Advertising> Advertisings { get; set; }
    public DbSet<FileModel> Files { get; set; }
    public DbSet<RecipeLike> RecipeLikes { get; set; }
    public DbSet<RecipeDislike> RecipeDislikes { get; set; }
    public DbSet<Bookmark> Bookmarks { get; set; }
    public DbSet<Purchase> Purchases { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql(_connectionString);
    }
}

and appsettings.json

{
  "ConnectionStrings": {
    "Recipes" : "connection string is here"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

Upvotes: 0

Views: 3356

Answers (2)

Joe Robe
Joe Robe

Reputation: 1195

Just adding here an additional scenario where this happens. If you have two different context (in my case I have one for MS SQL and one for MySQL). I did run add-migration for MS SQL but you also need to run add-migration for the other context so you end up with two migration folders. Then you can use update-database passing in the context name and it will update correctly.

Upvotes: 0

Aleksej_Shherbak
Aleksej_Shherbak

Reputation: 3068

Ok, I've solved it for my case. It was .Designer.cs files, that I've lost.

If you lose it you will have the same problem

enter image description here

Upvotes: 3

Related Questions