C# 用于数据库优先方法的 EF 迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9255190/
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
EF Migrations for Database-first approach?
提问by Shaddix
We're using Database first approach with EntityFramework.
We've several customers, and when we deploy new product version, we're now applying DB schema changes "manually" with tools like SQL Compare.
我们在 EntityFramework 中使用数据库优先方法。我们有几个客户,当我们部署新的产品版本时,我们现在使用诸如SQL Compare.
Is there a way how EF Migrations could help to apply changes to customers DB automatically?
EF Migrations 有没有办法帮助自动将更改应用到客户数据库?
采纳答案by Paul
As far as I know, EF Migrations is a product targeted at CodeFirst and doesn't support Database First operations.
据我所知,EF Migrations 是针对 CodeFirst 的产品,不支持 Database First 操作。
CodeFirst assumes that you will never make any changes manually to the database. All the changes to the database will go through the code first migrations.
CodeFirst 假定您永远不会对数据库进行任何手动更改。对数据库的所有更改都将通过代码优先迁移。
回答by Amin Saqi
I think there is! You need to continue your way through the code first.
我认为有!您需要先继续阅读代码。
To do this, Suppose that you have the following DbContext that EF Db first created for you:
为此,假设您有以下 EF Db 首先为您创建的 DbContext:
public class MyDbContext : DbContext
{
public MyDbContext()
: base("Name=DefaultConnection")
{
}
// DbSets ...
}
change that to the following to start using code first and all magic tools of it (migration, etc.):
将其更改为以下内容以开始使用代码及其所有魔术工具(迁移等):
public class MyDbContext : DbContext
{
public MyDbContext()
: base("YourDbFileName")
{
}
// DbSets ...
}
It causes that EF creates a new connection string using SQL Express on your local machine in your web.config file with the name YourDbFileName, something just like the early DefaultConnection Db first created.
这会导致 EF 在您的 web.config 文件中使用本地计算机上的 SQL Express 创建一个新的连接字符串,名称为 YourDbFileName,就像最初创建的早期 DefaultConnection Db 一样。
All you may need to continue your way, is that edit the YourDbFileName ConStr according to your server and other options.
您可能需要继续您的方式,就是根据您的服务器和其他选项编辑 YourDbFileName ConStr。
回答by Gabriel Andrés Brancolini
Just look for your DbContext child object and look for this method:
只需查找您的 DbContext 子对象并查找此方法:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
If you comment this:
如果您对此发表评论:
throw new UnintentionalCodeFirstException();
then the exception would not be thrown on migration operation. As you might imagine, the migration look for this part to know what are the configurations for each entity with what table or tables.
那么在迁移操作时不会抛出异常。正如您可能想象的那样,迁移查找这部分以了解每个实体与哪些表或哪些表的配置。
Sorry if I didn't go with more details, if you wish more, I'll be happy to edit this and make it better!
对不起,如果我没有提供更多细节,如果你想要更多,我很乐意编辑它并使它变得更好!
回答by tatigo
Starting Entity Framework 4.1 you can do Code First Migrations with an existing database.
从 Entity Framework 4.1 开始,您可以使用现有数据库进行代码优先迁移。
So first you have the database, create the model, enable migrations.
所以首先你有数据库,创建模型,启用迁移。
The most important thing to remember that you should run Enable-Migrations before you do any changes to the schema since it should be in sync between your db and code.
要记住的最重要的事情是,您应该在对架构进行任何更改之前运行 Enable-Migrations,因为它应该在您的数据库和代码之间同步。

