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();
}
}
}
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;
(from https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=dotnet-core-cli)