如何忽略EF 4.3迁移中的表/类 [英] How to ignore a table/class in EF 4.3 migrations

查看:134
本文介绍了如何忽略EF 4.3迁移中的表/类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用EF 4.3(beta)进行测试



我有一些新的类应该生成db表和列。



从一个旧项目中,我在我的架构中有一些旧表,我想通过EF访问。
所有类都被声明。对于访问旧表,有一个poco映射。



db迁移也尝试创建该旧表。



如何设置这个类/表不是迁移的一部分,而是ef模型的一部分?

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

从模型中删除整个类。最后我不能使用它通过dbContext访问。



我喜欢使用自动迁移。
我尽量避免将旧数据库表完全迁移到EF类。 (是的,我知道有发电机)
有120个表,它们仍然被旧的应用程序使用。



只有一些新的表与EF(新应用程序)一起使用。
有3个常用的表。
那些不应该创建但不能通过ef访问。

解决方案

使用EF 4.3.1 内置支持这种情况。当添加映射到数据库中现有表的类时,请使用 -IngoreChanges 切换到 Add-Migration 。 / p>

这将生成一个空的迁移,更新的元数据签名包含新添加的类。



通常这是在启动使用EF迁移时完成的,因此初始迁移名称为:

 添加迁移InitialMigration -IgnoreChanges 


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.

解决方案

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 -IngoreChanges 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

这篇关于如何忽略EF 4.3迁移中的表/类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆