EF Core understanding migrations


I encountered some weird behaviour with EF Core 3.1 that made me wondering what is actually happening. So hopefully someone can explain this to me in all fine details.


Let's say you have two projects. Project A (main) and project B (side-project). 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.

When executingdotnet 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.

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:

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

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.

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....?


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. Apparently this field was empty... But it leaves me with 1 question.

Why did the add command DID work without a port specified and was only the remove command complaining about the connection?


And yet another new finding, when adding the following method to the AppDbContext class:

protected override void OnConfiguring(DbContextOptionsBuilder options)

The add command works fine, but the remove command complains about the incorrect format of the connectionstring

System.ArgumentException: Format of the initialization string does not conform to specification starting at index 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??? It does nothing with the DB, right?

asked on Stack Overflow Dec 10, 2020 by Svenmarim • edited Dec 10, 2020 by Svenmarim

1 Answer


As an answer to my own question, here an explanation on how the add and remove migrations work.

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.

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. Because the remove functionality can only remove PENDING migrations. So not any migrations that have already been applied to the DB.

answered on Stack Overflow Dec 10, 2020 by Svenmarim

User contributions licensed under CC BY-SA 3.0