1

I have an ASP.NET MVC EF code first project. I copied it and made another project from it. I didn't change any of the namespaces or project names. I copied the existing DB and made a copy with a different name. Code first migrations work fine in the original project. In the new copied project they only pickup changes to existing models like adding new fields or changing field names. If I try to add a new model class, Add-Migration doesn't pick up that change.

I've tried this but it didn't work. I tried this and it didn't work.

I've tried to delete the migration history folder and dbo._MigrationHistory table data and recreate migrations using Enable-Migrations. I've tried to delete existing migrations in the migration folder and create another initial migration using Add-Migration Initial -IgnoreChanges then update-database. Then add my changes. None of this worked.

Heinrich
  • 1,711
  • 5
  • 28
  • 61

1 Answers1

1

YOU will need to run Enable-Migrations –EnableAutomaticMigrations.

Now say you need to add Address table to your database code first:

Create your new model Address

Create your Configuration file, something

class AddressConfiguration : EntityTypeConfiguration<Address>
{
    public AddressConfiguration()
        : base()
    {
        HasKey(p => p.AddressId);
        ToTable("Address");    

    }
}

Add your DbSet and modelBuilder data in your main XYZContext.cs file

DbSet

public DbSet<Address> Address {get; set;}

modelBuilder

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new AddressConfiguration());
}

Run update-database via Visual Studio's Package Manager Console, make sure you have selected the correct project from the drop down, this is likely to be a .Repository named project.

Your new table should now exist in your database.

delta12
  • 97
  • 1
  • 8
  • I didn't need to EnableAutomaticMigrations but the rest of this is what I did, same the comment from @SteveGreen – Heinrich Apr 13 '19 at 00:15