We are using Entity Framework Code First, and have our models in C#. One of our DBAs added an additional column, how do we migrate his change into our Net Core Project? Is there a command line to automatically sync this?
我们正在使用Entity Framework Code First,并在C#中使用我们的模型。我们的一位DBA增加了一个专栏,我们如何将他的变更迁移到我们的Net Core项目中?是否有命令行自动同步?
We know command line below will take a whole database and place into C#. We are only interested in small modified changes.
我们知道下面的命令行将占用整个数据库并放入C#。我们只对小修改后的变化感兴趣。
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
2 个解决方案
#1
1
That's all you've got. Using an existing database is an all or nothing affair. Either you manage your entities via code and migrate changes there back to your database, or you scaffold the entities from your database. In other words, a database change means you need to re-scaffold everything. There's no such thing as "code migration".
这就是你所拥有的一切。使用现有数据库是一个全有或全无的事情。您可以通过代码管理实体并将更改迁移回数据库,也可以从数据库中构建实体。换句话说,数据库更改意味着您需要重新构建所有内容。没有“代码迁移”这样的东西。
Alternatively, I suppose you could just manually modify the entity class. For simple changes like adding a new column, that's probably the best approach, as long as you take care to replicate the property exactly as it should be to match the column in the database.
或者,我想您可以手动修改实体类。对于像添加新列这样的简单更改,这可能是最好的方法,只要您注意完全按照应该匹配数据库中的列的方式复制属性。
EDIT (based on comments on question)
编辑(基于对问题的评论)
Okay, so if you're doing code-first, then no one should be manually touching the database ever. Period. You have to just make that a rule. If the DBA wants to make a change, he can communicate that to your team, where you can make the appropriate code change and hand back a SQL script to perform the migration. This is where DevOps comes into play. Your DBA should be part of your team and you all should be making decisions together. If you can't do that, this isn't going to work.
好的,所以如果你在做代码优先,那么任何人都不应该手动触摸数据库。期。你必须要做到这一点。如果DBA想要进行更改,他可以将其传达给您的团队,您可以在其中进行适当的代码更改并交回SQL脚本以执行迁移。这就是DevOps发挥作用的地方。您的DBA应该是您团队的一部分,您应该共同做出决策。如果你不能这样做,这是行不通的。
#2
1
- Add column to the model.
-
Generate migration.
Migration file20180906120000_add_dba_column.cs
will be generated生成迁移。将生成迁移文件20180906120000_add_dba_column.cs
-
Manually add new record about just created migration in table
_EFMigrationHistory
record:MigrationId: 20180906120000_add_dba_column, ProductVersion: <copy from previous migration>
在表_EFMigrationHistory记录中手动添加有关刚刚创建的迁移的新记录:MigrationId:20180906120000_add_dba_column,ProductVersion: <从以前的迁移中复制>
将列添加到模型中。
If you want do it automatically, means you want combine two different approaches in same time.
如果您想自动执行,意味着您希望同时组合两种不同的方法。
I think there was a reason why EF team separated "EF Code First" and "EF Database first" approaches.
我认为EF团队之所以分离“EF Code First”和“EF Database first”方法是有原因的。
When you choose EF Code first
approach, that means that source of truth is your application code.
If you want stick to have code-first approach, that mean you should train your DBA to write EF migrations.
当您选择EF Code第一种方法时,这意味着事实来源是您的应用程序代码。如果你想坚持采用代码优先的方法,那就意味着你应该培训你的DBA来编写EF迁移。
#1
1
That's all you've got. Using an existing database is an all or nothing affair. Either you manage your entities via code and migrate changes there back to your database, or you scaffold the entities from your database. In other words, a database change means you need to re-scaffold everything. There's no such thing as "code migration".
这就是你所拥有的一切。使用现有数据库是一个全有或全无的事情。您可以通过代码管理实体并将更改迁移回数据库,也可以从数据库中构建实体。换句话说,数据库更改意味着您需要重新构建所有内容。没有“代码迁移”这样的东西。
Alternatively, I suppose you could just manually modify the entity class. For simple changes like adding a new column, that's probably the best approach, as long as you take care to replicate the property exactly as it should be to match the column in the database.
或者,我想您可以手动修改实体类。对于像添加新列这样的简单更改,这可能是最好的方法,只要您注意完全按照应该匹配数据库中的列的方式复制属性。
EDIT (based on comments on question)
编辑(基于对问题的评论)
Okay, so if you're doing code-first, then no one should be manually touching the database ever. Period. You have to just make that a rule. If the DBA wants to make a change, he can communicate that to your team, where you can make the appropriate code change and hand back a SQL script to perform the migration. This is where DevOps comes into play. Your DBA should be part of your team and you all should be making decisions together. If you can't do that, this isn't going to work.
好的,所以如果你在做代码优先,那么任何人都不应该手动触摸数据库。期。你必须要做到这一点。如果DBA想要进行更改,他可以将其传达给您的团队,您可以在其中进行适当的代码更改并交回SQL脚本以执行迁移。这就是DevOps发挥作用的地方。您的DBA应该是您团队的一部分,您应该共同做出决策。如果你不能这样做,这是行不通的。
#2
1
- Add column to the model.
-
Generate migration.
Migration file20180906120000_add_dba_column.cs
will be generated生成迁移。将生成迁移文件20180906120000_add_dba_column.cs
-
Manually add new record about just created migration in table
_EFMigrationHistory
record:MigrationId: 20180906120000_add_dba_column, ProductVersion: <copy from previous migration>
在表_EFMigrationHistory记录中手动添加有关刚刚创建的迁移的新记录:MigrationId:20180906120000_add_dba_column,ProductVersion: <从以前的迁移中复制>
将列添加到模型中。
If you want do it automatically, means you want combine two different approaches in same time.
如果您想自动执行,意味着您希望同时组合两种不同的方法。
I think there was a reason why EF team separated "EF Code First" and "EF Database first" approaches.
我认为EF团队之所以分离“EF Code First”和“EF Database first”方法是有原因的。
When you choose EF Code first
approach, that means that source of truth is your application code.
If you want stick to have code-first approach, that mean you should train your DBA to write EF migrations.
当您选择EF Code第一种方法时,这意味着事实来源是您的应用程序代码。如果你想坚持采用代码优先的方法,那就意味着你应该培训你的DBA来编写EF迁移。