Entity Framework Core 3.0, No Migrations Were Applied. The database is already up to date. NOT Working

5.8k Views Asked by At

I'm trying to update the database using migrations, but in the attempt I receive the following message 'No migrations were applied. The database is already up to date'. I know that indicates that there has been no changes in the code to apply a certain update to the database, but it shows the message even when the code has been edited to make a change in the database.

Note: The command 'Add-Migrations' works perfectly.

I leave some related code.

Beforehand, Thank You!!

namespace MasterGym.Persistence.Models
{
    public class MasterGymContextFactory : IDesignTimeDbContextFactory<MasterGymContext>
    {
        public MasterGymContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("AppSettings.json").Build();
            var connectionString = configuration.GetConnectionString("MasterGymModel");
            var builder = new DbContextOptionsBuilder<MasterGymContext>();
            builder.UseSqlServer(connectionString);

            return new MasterGymContext(builder.Options);
        }
    }
}

namespace MasterGym.Persistence.Models
{
    public class MasterGymContext : DbContext
    {
        public MasterGymContext(DbContextOptions<MasterGymContext> options) : base(options)
        {
        }

        public DbSet<Customer> Customers { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<Phone> Phones { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<EmergencyContact> EmergencyContacts { get; set; }
        public DbSet<EmergencyDoctor> EmergencyDoctors { get; set; }
        public DbSet<MedicalQuestionnaire> MedicalQuestionnaires { get; set; }
        public DbSet<GeneralQuestionnaire> GeneralQuestionnaires { get; set; }
        public DbSet<DiseaseQuestionnaire> DiseaseQuestionnaires { get; set; }
        public DbSet<InjuryQuestionnaire> InjuryQuestionnaires { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfigurationsFromAssembly(typeof(MasterGymContext).Assembly);
        }
    }
}

namespace MasterGym.Persistence.Models.EntityMaps.Customers.MedicalQuestionnaires
{
    internal class GeneralQuestionnaireMap : IEntityTypeConfiguration<GeneralQuestionnaire>
    {
        public void Configure(EntityTypeBuilder<GeneralQuestionnaire> entity)
        {
            entity.ToTable("GeneralQuestionnaires");
            entity.HasKey(gq => gq.Id);
            entity.Property(gq => gq.Id).HasColumnName("Id").ValueGeneratedOnAdd();
            entity.Property(gq => gq.IsPrivateHealthInsurance).HasColumnName("IsPrivateHealthInsurance").HasColumnType("bit").IsRequired();
            entity.Property(gq => gq.HealthInsuranceFoundation).HasColumnName("HealthInsuranceFoundation").HasColumnType("varchar").HasMaxLength(60).IsRequired();
            entity.Property(gq => gq.BloodType).HasColumnName("BloodType").HasColumnType("int").IsRequired();
            entity.Property(gq => gq.IsPregnant).HasColumnName("IsPregnant").HasColumnType("bit").IsRequired();
            entity.Property(gq => gq.PregnancyWeeks).HasColumnName("PregnancyWeeks").HasColumnType("int").IsRequired();
            entity.Property(gq => gq.IsRegularPhysicalActivity).HasColumnName("IsRegularPhysicalActivity").HasColumnType("bit").IsRequired();
            entity.Property(gq => gq.RegularPhysicalActivityPerWeek).HasColumnName("RegularPhysicalActivityPerWeek").HasColumnType("int").IsRequired();
            entity.Property(gq => gq.IsSurgeryInTheLastFiveYears).HasColumnName("IsSurgeryInTheLastFiveYears").HasColumnType("bit").IsRequired();
            entity.Property(gq => gq.SurgeryTypeAndWhen).HasColumnName("SurgeryTypeAndWhen").HasColumnType("varchar").HasMaxLength(100).IsRequired();
            entity.Property(gq => gq.IsSmoker).HasColumnName("IsSmoker").HasColumnType("bit").IsRequired();
            entity.Property(gq => gq.SmokerForHowLong).HasColumnName("SmokerForHowLong").HasColumnType("int").IsRequired();
            entity.Property(gq => gq.IsAnyMedication).HasColumnName("IsAnyMedication").HasColumnType("bit").IsRequired();
            entity.Property(gq => gq.MedicationTypeAndWhen).HasColumnName("MedicationTypeAndWhen").HasColumnType("varchar").HasMaxLength(100).IsRequired();
            entity.Property(gq => gq.AnythingElseToKnow).HasColumnName("AnythingElseToKnow").HasColumnType("varchar").HasMaxLength(256).IsRequired();
            entity.Property(gq => gq.MedicalQuestionnaireId).HasColumnName("MedicalQuestionnaireId").HasColumnType("int").IsRequired();
        }
    }
}
3

There are 3 best solutions below

5
Jeremy Lakeman On

If you have your migrations stored in a different assembly (as it seems from the comments on the question), then you need to specify this assembly when you connect to the database;

options.UseSqlServer(
    connectionString,
    x => x.MigrationsAssembly("MyApp.Migrations"));

(from https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=dotnet-core-cli)

0
Slideshow Bob On

Common Problem: if you use dotnet ef migrations add xxxx the generated files are not attached to the project. So when you run the database update, there are no new migration files in the project.

The solution is to go to your Visual Studio IDE and "Add->Existing" and attach the generated file. Then run update and that would work.

0
Oleg Schastlyvyi On

Try to move migration run to the Database context constructor:

public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) 
{ 
   Database.Migrate(); 
}

If you got error: "Could not load file or assembly 'Microsoft.EntityFrameworkCore.Relational'" add it to the startup project. EF Core NuGet Packages