C# 如何强制 EF 代码首先重新创建数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19118953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force EF code first to recreate databases?
提问by Pinch
I had a bunch of tables Code First created.
我有一堆表 Code First 创建的。
Then in SQL i deleted one table so that i could inevitably ask this question on stack.
然后在 SQL 中我删除了一个表,这样我就可以不可避免地在堆栈上问这个问题。
Upon using update-database
in package management console I get:
update-database
在包管理控制台中使用后,我得到:
Cannot find the object "dbo.ContractParents" because it does not exist or you do not have permissions.
找不到对象“dbo.ContractParents”,因为它不存在或您没有权限。
What is the best way to recreate my table?
重新创建我的表的最佳方法是什么?
I've read up about context.Database.CreateIfNotExists();
我已经阅读了 context.Database.CreateIfNotExists();
I put it in my seed function, but nothing doing.
我把它放在我的种子函数中,但什么也没做。
Thanks!
谢谢!
采纳答案by ledgeJumper
To explain what happens with your update-database command and why the context.Database.CreateIfNotExists()
method in the seed did not work:
要解释您的 update-database 命令会发生什么以及为什么context.Database.CreateIfNotExists()
种子中的方法不起作用:
When you run the update-database command it first looks at your connection string to see if the database is there. If it is it looks at the migration history table and checks that against what is in you DbContext
class. If it sees that there are tables missing or changes it will attempt to update the database. The Seed method is not called until after that is done, so that is why that did not work.
当您运行 update-database 命令时,它首先查看您的连接字符串以查看数据库是否存在。如果是,它会查看迁移历史表并根据您的DbContext
课程中的内容进行检查。如果它发现有表丢失或更改,它将尝试更新数据库。直到完成之后才会调用 Seed 方法,所以这就是不起作用的原因。
While developing using EF-Code First I usually approach the problem in a few different ways depending on how large my database is. I usually went the route of deleting all the tables (including the migration history table) and then running the update-database command again. Works fine, just really time consuming if you have a lot of tables with a lot of FK constraints on it.
在使用 EF-Code First 进行开发时,我通常会根据数据库的大小以几种不同的方式解决问题。我通常走删除所有表(包括迁移历史表)然后再次运行 update-database 命令的路线。工作正常,如果你有很多表有很多 FK 约束,那真的很耗时。
I finally became tired of it and found these scriptsto make the dropping of tables exponentially faster. I went to this because I was running my app on Azure. When I am running it on my local machine, I would just delete the whole database and make a brand new database with the same name.
我终于厌倦了它,并发现这些脚本可以使表格的删除速度呈指数级增长。我去这里是因为我在 Azure 上运行我的应用程序。当我在本地机器上运行它时,我只会删除整个数据库并创建一个具有相同名称的全新数据库。
Elegant solution? No. Does it work? More or less...
优雅的解决方案?不,它有效吗?或多或少...
回答by Pinch
For a quick and dirty approach, that'll get you home for dinner on time along with lots of dataloss (i'm still in beta using test data)
对于快速而肮脏的方法,这将使您准时回家吃晚饭以及大量数据丢失(我仍在使用测试数据进行测试)
drop the dbo.__MigrationHistory system table
删除 dbo.__MigrationHistory 系统表
along with all of your other tables.
以及所有其他表。
Back up your data first!
先备份数据!
update-database -verbose
(you may need some sauce with your spaghetti)
update-database -verbose
(你的意大利面可能需要一些酱汁)
I am not impressed, but it works.
我没有留下深刻的印象,但它有效。
Hopefully someone will come up with a better answer in the future.
希望将来有人会提出更好的答案。
It would help to really understand migrations better.
这将有助于更好地真正理解迁移。
回答by Steve
For yet another cheesy option...
对于另一个俗气的选择......
Right click on your db in server explorer, and hit delete Then you can do
在服务器资源管理器中右键单击您的数据库,然后点击删除然后您可以执行
Enable-Migrations -EnableAutomaticMigrations -Force
Update-Database -Force
Dirty update, clean result :)
脏更新,干净的结果:)