简体   繁体   English

在运行更新数据库时,如何使用EF 5 Code First映射现有表并且_do_不创建/触摸它?

[英]How to map existing table with EF 5 Code First and _do_ not create/touch it when running update-database?

I have an EF 5 Code First VS 2012 project and some entites. 我有一个EF 5 Code First VS 2012项目和一些实体。 I use update-database to generate/seed the tables and data. 我使用update-database来生成/种子表和数据。 However I have 4 existing tables with data, and I would like to map this tables to 4 POCO class, because I would like to access them via DbContext. 但是,我有4个已有数据的表,我想将此表映射到4个POCO类,因为我想通过DbContext访问它们。

My problem if I create the entity POCO classes, and add the approprite 我的问题是,如果我创建实体POCO类,并添加适当的内容

public DbSet<MyExternal> MyExternals { get; set; }

property to my DbContext class, when I run the update-database it tries to create the tables and exits with error "Table MyExternal is exists" or similar. 属性设置为DbContext类的属性,当我运行更新数据库时,它会尝试创建表并退出并出现错误“表MyExternal存在”或类似错误。 I understand why is that, but my question: How to explain to the update-database process to not create/alter these tables? 我理解为什么会这样,但是我的问题是:如何向更新数据库过程说明不创建/更改这些表?

Note: I know how to map tables to different names than class name etc, not this is my problem. 注意:我知道如何将表映射到与类名等不同的名称,这不是我的问题。

I know this question is rather old and probably answered by yourself, but the solution might be to create an empty migration: 我知道这个问题比较老,可能自己回答了,但是解决方案可能是创建一个空迁移:

public override void Up()
{
}

public override void Down()
{
}

Afterwards your Migration has stored the correct database model (in which your four tables are known) and everything should work out just fine 之后,您的迁移已存储了正确的数据库模型(已知您的四个表),并且一切正常

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

相关问题 EF代码优先update-database -script未运行Seed()方法 - EF Code First update-database -script not running Seed() method 运行更新数据库时,EF代码优先“字符串或二进制将被截断” - EF Code First “String or binary would be truncated” when running Update-Database EF6代码优先:更新数据库登录失败 - EF6 Code First: Login Failing on update-database EF6代码优先更新数据库,不删除数据 - EF6 Code First update-database without deleting data 如何在调用“更新数据库”之前查看“ EF代码优先迁移”更改 - How to view the EF Code First Migrations changes before calling 'Update-Database' VSTS Build如何首先为EF核心代码添加update-database命令 - VSTS Build How to Add update-database command for EF Core Code First 使用Entity Framework,Code First(POCO)和Code Contracts运行Update-Database时出错 - Error When running Update-Database using Entity Framework, Code First (POCO) and Code Contracts 运行代码第一次迁移update-database时出错 - Error running code first migration update-database 更新数据库将索引添加到表中,并且EF代码优先-超时已过期。 完成之前经过的超时时间 - Update-Database Adding Index To Table And EF Code First - Timeout expired. The timeout period elapsed prior to completion 使用代码优先工作流使用 Update-Database 时出现异常 - Exception when using Update-Database using code first workflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM