实体框架核心3.0,未应用任何迁移。该数据库已经是最新的。无法运作

时间:2019-12-16 01:03:17

标签: c# entity-framework-core entity-framework-core-3.0 entity-framework-migrations

我正在尝试使用迁移来更新数据库,但是在尝试中,我收到以下消息“未应用任何迁移。数据库已经是最新的。我知道这表明没有对数据库进行特定更新的代码更改,但是即使对代码进行了编辑以对数据库进行更改,它也会显示该消息。

注意:“添加迁移”命令可以正常工作。

我留下一些相关代码。

在此之前,谢谢您!

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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果您将迁移存储在其他程序集中(从问题注释中可以看出),那么在连接到数据库时需要指定此程序集;

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

(来自https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects?tabs=dotnet-core-cli