Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a table/class in EF 4.3 migrations

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.

like image 344
coding Bott Avatar asked Jan 26 '12 10:01

coding Bott


People also ask

Why can't Entity Framework migrations drop table?

Entity Framework Migrations can't drop table because of foreign key constraint Hot Network Questions Generate a regular graph Pursuing a less exciting PhD research topic at a top-ranked university VS doing more interesting research at a lesser-known university?

Should EF take control of a specific table?

However, the answer you are referring to works only if you have an existing schema and then decide that EF should take control over it. In my case, I want EF to never touch that particular table. The schema of that table will evolve through the time but outside EF.

How to migrate a table from one database to another?

When you use migration, it will create a new table to database and it will tell you that the table already exists. If so, you can refer to the following steps: (1) Using Code First to connect the database. Then in the intial.cs, remove all code that duplicates existing tables from inside the Up and Down override methods in the initial migration.

What is the name of EF migration add-migration?

Usually this is done when starting using EF Migrations, hence the "InitialMigration" name: Add-Migration InitialMigration –IgnoreChanges


1 Answers

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
like image 84
Anders Abel Avatar answered Oct 16 '22 15:10

Anders Abel