简体   繁体   English

在LocalDB中重置EF代码优先数据库

[英]Reset EF code-first database in LocalDB

I am using Entity Framework 5.0 RC with the LocalDB database that comes pre-configured with VS 2012 RC for some prototyping and testing of code-first database. 我正在使用Entity Framework 5.0 RC与预配置了VS 2012 RC的LocalDB数据库进行一些原型设计和代码优先数据库的测试。

After a few cycles of changing my code-first database and running "update-database" I did a huge breaking change and automatic migration failed bad. 经过几个周期的更改我的代码优先数据库和运行“更新数据库”后,我做了一个巨大的改变,自动迁移失败了。 I don't care about resolving this conflict since this is just prototyping, so losing data is fine. 我不关心解决这个冲突,因为这只是原型设计,所以丢失数据很好。

How do I reset or delete the database? 如何重置或删除数据库? Reset must also reset the migration table, which seems to be hidden from Server Explorer in Visual Studio. 重置还必须重置迁移表,这似乎在Visual Studio中的服务器资源管理器中隐藏。

Drop the whole database in Visual Studio 在Visual Studio中删除整个数据库

  1. Open SQL Server Object Explorer 打开SQL Server对象资源管理器
  2. Find the database you are working with Right click and select delete 找到您正在使用的数据库右键单击并选择删除
  3. Select the two check boxes (Delete backup and restore history, and Close existing connections) and click ok 选中两个复选框(删除备份和还原历史记录,然后关闭现有连接),然后单击“确定”
  4. Run program again and it should recreate the dabase 再次运行程序,它应该重新创建dabase

So far this is the best approach I have found. 到目前为止,这是我发现的最佳方法。

  1. Make __MigrationHistory a non-system table. 使__MigrationHistory成为非系统表。
  2. Execute SQL script to drop all tables. 执行SQL脚本以删除所有表。

To alter __MigrationHistory table, see this article to handle both existing databases and new databases created with code-first. 要更改__MigrationHistory表,请参阅此文章以处理现有数据库和使用代码优先创建的新数据库。

The SQL script is as follows: SQL脚本如下:

EXEC sp_MSforeachtable @command1 = "DROP TABLE ?";

I am using Database .NET v4 Free to run this SQL script, because my Visual Studio 11 RC copy won't let me run queries on LocalDB for some reason. 我使用Database .NET v4 Free来运行这个SQL脚本,因为我的Visual Studio 11 RC副本不允许我出于某种原因在LocalDB上运行查询。 Also, I need to run the script twice for it to drop all tables due to some foreign keys. 此外,我需要运行该脚本两次,因为一些外键会丢弃所有表。 I haven't bothered fixing this since it seems to work well enough as it is. 我没有费心去解决这个问题,因为它似乎工作得很好。

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

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