简体   繁体   English

EF Core 理解迁移

[英]EF Core understanding migrations

I encountered some weird behaviour with EF Core 3.1 that made me wondering what is actually happening.我在 EF Core 3.1 中遇到了一些奇怪的行为,这让我想知道实际发生了什么。 So hopefully someone can explain this to me in all fine details.所以希望有人可以详细地向我解释这一点。

Scenario设想

Let's say you have two projects.假设您有两个项目。 Project A (main) and project B (side-project).项目 A(主要)和项目 B(副项目)。 For both projects you can add/remove migrations, as project A is set-up to get all pending migrations from project B on start-up and execute them.对于这两个项目,您都可以添加/删除迁移,因为项目 A 已设置为在启动时从项目 B 获取所有待处理的迁移并执行它们。

When executing dotnet ef migrations add Test -c AppDbContext -o DbContexts/Migrations/AppDb on project A and after completion (BEFORE even applying it to the DB) execute dotnet ef migrations remove -c AppDbContext , everything works as expected and the migration gets removed.在项目 A 上执行dotnet ef migrations add Test -c AppDbContext -o DbContexts/Migrations/AppDb并在完成后(甚至在将其应用到数据库之前)执行dotnet ef migrations remove -c AppDbContext ,一切正常,迁移被删除.

For project B on the otherhand after succesfully executing the add command, the moment that I try to execute the remove command (also BEFORE even applying it to the DB), I get the following error:另一方面,对于成功执行add命令后的项目 B,在我尝试执行remove命令的那一刻(甚至在将其应用于 DB 之前),我收到以下错误:

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. Microsoft.Data.SqlClient.SqlException (0x80131904):与 SQL 服务器建立连接时发生与网络相关或特定于实例的错误。 The server was not found or was not accessible.服务器未找到或无法访问。 Verify that the instance name is correct and that SQL Server is configured to allow remote connections.验证实例名称是否正确,并且 SQL 服务器配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供者,错误:40 - 无法打开与 SQL 服务器的连接)

So I took the error message as an advice and started to follow this guide to allow remote access to the database.因此,我将错误消息作为建议,并开始按照本指南允许远程访问数据库。 After the guide, the remove command on project B was also successfully executed.引导之后,项目B上的remove命令也成功执行了。

But now I am wondering... Why wasn't EF Core connecting to the (LOCAL) DB before I followed the guide and DID it work on project A, but not on B....?但现在我想知道......为什么在我按照指南操作之前 EF Core 没有连接到(本地)数据库,并且它在项目 A 上工作,而不是在 B 上工作......?

EDIT编辑

After some more research, I discovered the specific action from the guide that helped me to resolve the error that I got.经过更多研究,我从指南中发现了帮助我解决错误的具体操作。 It was to set the TCP Port to 1433 in the Sql Server Configuration Manager.在 Sql 服务器配置管理器中将 TCP 端口设置为 1433。 Apparently this field was empty... But it leaves me with 1 question.显然这个字段是空的......但它给我留下了 1 个问题。

Why did the add command DID work without a port specified and was only the remove command complaining about the connection?为什么add命令 DID 在没有指定端口的情况下工作,而只是remove命令抱怨连接?

EDIT2编辑2

And yet another new finding, when adding the following method to the AppDbContext class:还有一个新发现,将以下方法添加到AppDbContext class 时:

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
    options.UseSqlServer("foo");
    base.OnConfiguring(options);
}

The add command works fine, but the remove command complains about the incorrect format of the connectionstring add命令工作正常,但remove命令抱怨连接字符串的格式不正确

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0. System.ArgumentException:初始化字符串的格式不符合从索引 0 开始的规范。

So we can conclude from this that the add command DOESN'T require a valid connectionstring (as it does nothing with the DB), while the remove command DOES require a valid connectionstring... but why???所以我们可以由此得出结论, add命令不需要有效的连接字符串(因为它对数据库没有任何作用),而remove命令需要有效的连接字符串......但是为什么? It does nothing with the DB, right?它对数据库没有任何作用,对吧?

As an answer to my own question, here an explanation on how the add and remove migrations work.作为对我自己的问题的回答,这里解释了addremove迁移的工作原理。

The add migration functionality DOES require a valid string as the connectionstring in the context, but it DOESN'T need to be a correct connectionstring, as EF Core is not realy connecting to the DB. add迁移功能确实需要一个有效的字符串作为上下文中的连接字符串,但它不需要是正确的连接字符串,因为 EF Core 并未真正连接到数据库。

For the remove migration functionality, you DO require a correct connectionstring, as EF Core makes a connection to the DB to compare the local list of migrations with the _EFMigrationsHistory table to check if there are any migrations that has not been applied to the DB.对于remove迁移功能,您需要正确的连接字符串,因为 EF Core 会连接到数据库以将本地迁移列表与_EFMigrationsHistory表进行比较,以检查是否有任何迁移尚未应用于数据库。 Because the remove functionality can only remove PENDING migrations.因为remove功能只能删除 PENDING 迁移。 So not any migrations that have already been applied to the DB.因此,没有任何已应用于数据库的迁移。

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

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