如何执行到两个数据库的迁移,一个用于测试,另一个用于EF中的一个项目?

发布于 2025-02-13 15:59:32 字数 1325 浏览 1 评论 0 原文

我在Blazor Wasm,AppSettings.json文件中有一个项目,我有两个连接字符串,一个与开发数据库的点,另一个是生产数据库,生产数据库是开发数据数据库的副本。

AppSettings。 JSON文件:

 "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlConnectionDesarrollo": "Data Source=10.0.0.41;Initial Catalog=QaInventarios;User ID=Innovacion;Password=***;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    "SqlConnectionProduccion": "Data Source=10.0.0.41;Initial Catalog=Inventarios;User ID=Innovacion;Password=***;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
  },

启动文件:

services.AddDbContext<ApplicationDbContext>
                (options => options.UseSqlServer(Configuration.GetConnectionString("SqlConnectionDesarrollo")));  

问题是要执行迁移,我要做的是评论连接字符串,具体取决于我要使用的数据库并更改启动中的名称,我知道这是一个不好的练习,但我不知道如何优化这一点,然后执行:

Add-Migration NameMigration
Update-Database

要执行迁移,在开发数据库中它运行良好,创建了新的迁移并进行了更改,然后我运行 update-date-database 命令再次更新生产数据库和更改未进行,也没有添加新的迁移,并且没有进行更改,如果我删除任何使用 remove overmigration -force 然后更新的迁移,也会发生同样的事情。 -database

我的数据库中存在不一致之处,我该如何执行 迁移,两者的变化,并且它们保持完全相同?

I have a project in blazor wasm, in the appsettings.json file I have two connection strings, one points to the development database and the other to the production database, the production database is a copy of the database of development data.

appsettings. json file:

 "AllowedHosts": "*",
  "ConnectionStrings": {
    "SqlConnectionDesarrollo": "Data Source=10.0.0.41;Initial Catalog=QaInventarios;User ID=Innovacion;Password=***;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    "SqlConnectionProduccion": "Data Source=10.0.0.41;Initial Catalog=Inventarios;User ID=Innovacion;Password=***;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
  },

startup file:

services.AddDbContext<ApplicationDbContext>
                (options => options.UseSqlServer(Configuration.GetConnectionString("SqlConnectionDesarrollo")));  

the problem is that to perform a migration what I do is comment a connection string depending on the database I want to use and changing the name in the startup, I know it's a bad practice but I don't know how to optimize this, then I execute:

Add-Migration NameMigration
Update-Database

to perform a migration, in the development database it works fine, a new migration is created and the changes are made, then I run the Update-Database command again to update the production database and the changes are not made and are not added the new migration and the changes are not made, the same thing happens if I delete any migration with Remove-Migration -Force and then an Update-Database

there are inconsistencies in my database, how can I perform
migrations, changes in both and that they remain exactly the same ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2025-02-20 15:59:32

在EF Core 5.0中,在EF Core CLI工具中引入了连接字符串选项。

示例

开发的

dotnet ef数据库更新 - 连接&lt;开发连接字符串&gt;

和生产。

dotnet ef数据库更新 - connection&lt;生产连接字符串&gt;

关于 dotnet ef Update

In EF Core 5.0, a connection string option is introduced in the EF Core CLI tool.

Example

For Development

dotnet ef database update --connection <Development Connection string>

And for Production.

dotnet ef database update --connection <Production Connection string>

Documentation about dotnet ef update

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文