Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

c# - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...