简体   繁体   English

如何在EF 4.3迁移中忽略表/类

[英]How to ignore a table/class in EF 4.3 migrations

I'm testing with EF 4.3 (beta) 我正在使用EF 4.3(测试版)进行测试

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. 在一个旧项目中,我的架构中有一些旧表,我想通过EF访问这些表。 All Classes are declared. 声明所有类。 For accessing the old table, there is a poco which is mapped. 为了访问旧表,有一个映射的poco。

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? 如何设置这个类/表不是迁移的一部分,而是ef模型的一部分?

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

removes the entire class from model. 从模型中删除整个类。 finally i can't use it for access via dbContext. 最终我无法使用它通过dbContext访问。

i like to use automatic migrations. 我喜欢使用自动迁移。 i try to avoid to migrate old db tables completely to EF classes. 我尝试避免将旧的数据库表完全迁移到EF类。 (Yes, i know there are generators for that) there are 120 tables, which are still used by an old applications. (是的,我知道有生成器)有120个表,这些表仍被旧的应用程序使用。

some new tables which are only used with EF (new app). 一些仅与EF(新应用)一起使用的新表。 there are 3 common used tables. 有3个常用表。 those should not created but accessed via ef. 那些不应该创建,但是可以通过ef访问。

With EF 4.3.1 released there is built in support for this scenario. 随着EF 4.3.1发布,内置了对这种情况的支持。 When adding classes that are mapped to existing tables in the database, use the -IgnoreChanges switch to Add-Migration . 添加映射到数据库中现有表的类时,请使用-IgnoreChanges开关到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: 通常,这是在开始使用EF迁移时完成的,因此使用“ InitialMigration”名称:

Add-Migration InitialMigration –IgnoreChanges

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. 生成迁移后,您可以修改其源文件,并从Up方法中删除旧类的CreateTable代码。 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 编辑:我写了一个演练 ,以使用EF 4.3.1向现有项目中添加迁移

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM