【问题标题】:Entity Framework Core 3.0, No Migrations Were Applied. The database is already up to date. NOT WorkingEntity Framework Core 3.0,未应用迁移。数据库已经是最新的。不工作
【发布时间】:2019-12-16 01:03:17
【问题描述】:

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

注意:“Add-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();
        }
    }
}

【问题讨论】:

  • 如果Add-Migrations 工作,那么它将生成代码来执行迁移。您是否看到该代码已添加到您的解决方案中?
  • 嗨,是的,我已将三个类自动添加到解决方案中,即“InitialCreate.cs、InitialCreate.Designer.cs 和 MyContextModelSnapshot.cs”
  • 确认你编辑的模型已经在你的DbContext(MasterGymContext)中实现了
  • 嗨,是的,我确认了这一点,但不一样。它继续而不对数据库应用更改。感谢评论
  • 我在包含“IEntityTypeConfiguration”的类中尝试的另一件事是..将访问修饰符“内部”更改为“公共”。同样,我也尝试了另一种方法来实现方法 'modelBuilder.ApplyConfigurationsFromAssembly(typeof(MasterGymContext).Assembly)' for 'modelBuilder.ApplyConfiguration(new GeneralQuestionnaireMap())' 而不是,继续没有工作..相同的消息'没有应用迁移'。

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


【解决方案1】:

如果您将迁移存储在不同的程序集中(从问题中的 cmets 看来),那么您需要在连接到数据库时指定此程序集;

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

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

【讨论】:

  • 嗨,我在同一个程序集中使用“迁移”文件夹包含 DbContext。我的程序集如下..
  • 我的程序集如下..“MasterGym.Persistence”是整个项目,就像添加的任何文件夹一样,所以,我的程序集是:“MasterGym.Persistence.Migrations”和“MasterGym.Persistence”。楷模'。注意:我将应用程序名称添加到之前的每个项目层,例如:'MasterGym.Data'、'MasterGym.Persistence'、'MasterGym.Business'。
  • 我尝试了你给我看的选项,我写道:'builder.UseSqlServer(connectionString, x => x.MigrationsAssembly("MasterGym.Persistence.Migrations"));'但我收到:无法加载文件或程序集“MasterGym.Persistence,Culture=neutral,PublicKeyToken=null”。该系统找不到指定的文件。感谢您的评论
  • 使用程序集名称而不是命名空间。 builder.UseSqlServer(connectionString, x => x.MigrationsAssembly("MasterGym.Persistence"));
  • 您的迁移程序集是否依赖于您的项目?如果没有,它不会被复制到输出文件夹。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-09
  • 2017-05-16
  • 2018-03-20
  • 2019-01-11
  • 1970-01-01
  • 2011-01-12
相关资源
最近更新 更多