12

I'm testing with EF 4.3 (beta)

I have some new classes which should generate db tables and columns.

From a old project i have some old tables in my schema, which i want to access via EF. All Classes are declared. For accessing the old table, there is a poco which is mapped.

The db migrations tries to create that old table, too.

How can it set that this class/table is not part of the migration, but part of the ef model?

xxx.OnModelCreating()    
{
    modelBuilder.Ignore<myOldTableClass>();    
}

removes the entire class from model. finally i can't use it for access via dbContext.

i like to use automatic migrations. i try to avoid to migrate old db tables completely to EF classes. (Yes, i know there are generators for that) there are 120 tables, which are still used by an old applications.

some new tables which are only used with EF (new app). there are 3 common used tables. those should not created but accessed via ef.

coding Bott
  • 4,287
  • 1
  • 27
  • 44

2 Answers2

30

With EF 4.3.1 released there is built in support for this scenario. When adding classes that are mapped to existing tables in the database, use the -IgnoreChanges switch to Add-Migration.

This will generate an empty migration, with an updated meta-data signature that contains the newly added classes.

Usually this is done when starting using EF Migrations, hence the "InitialMigration" name:

Add-Migration InitialMigration –IgnoreChanges
PaulB
  • 962
  • 7
  • 15
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • I don't understand from this answer how to specify the class name to ignore. – Robert Noack Jun 14 '14 at 18:52
  • 1
    I don't know how this would possibly work when we just want to ignore a specific table (i.e an existing table to skip migration). modelBuilder.Ignore(); does not make sense becuase we want the binding. – Spock Jul 09 '14 at 23:21
  • @Spock How it would work is like this: You would first ensure that you don't have any model changes that need migrations added, and that the classes you want the migrations to ignore are set up. You then run `Add-Migration` with the `-IgnoreChanges` flag. This will create a migration that includes the new models in the snapshot but does not add migration steps for them. The outcome is that as long as you don't modify these "ignored models",they will continue to be excluded from future migrations. It's basically what you are doing in your blog, but without manually modifying the migration file. – JLRishe Dec 30 '14 at 18:41
2

The correct workflow in this case is creating first migration prior to adding changes (new classes), than adding new classes and after that creating new migration where you will have only new tables.

If you didn't use migrations so far the framework will generate migrations for all tables you have in the project because it believes you are creating initial migration. Once you have migration generated you can modify its source file and remove CreateTable code for old classes from Up method. The problem is you will probably have to do this in any subsequent migration.

Edit: I wrote a walkthrough for adding migrations to existing project with EF 4.3.1

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • The tables would be included in the metadata for that migration, so they would not reappear in subsequent migrations. – Anders Abel Mar 08 '12 at 19:05
  • Would this work with AutomaticMigrationEnabled = true? – Spock Jul 09 '14 at 23:24
  • @Spock Yes it should. Make sure your DB is up to date. Then add the classes you want to ignore. Then run `Add-Migration` with `IgnoreChanges` to create a code based migration that updates metadata without creating the tables. – Anders Abel Dec 30 '14 at 18:52
  • @LadislavMrnka Is there anyway to achieve this when doing the migration manually? I have the following situation http://stackoverflow.com/questions/28815309/configure-ef-to-not-drop-any-tables-in-migration – user2697817 Mar 02 '15 at 17:14