Running dotnet ef database update while using EF Core 8.0.11 or earlier used to set ASPNETCORE_ENVIRONMENT and DOTNET_ENVIRONMENT to "Development" if it was not explicitly specified. On updating to EF Core 9, those do not default if there is an implementation of IDesignTimeDbContextFactory. The version of dotnet-ef (at least between 9.0.0 and 8.0.11) does not seem to matter.
This project can be manipulated to reproduce the issue, though I've also included some relevant tool output from dotnet ef database update --project .\ReproTest\ --verbose below.
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8 and 9
Operating system: Windows 11 23H2, 22631.4460
IDE: Visual Studio 2022 17.12.0
Log output
EF Core 8.0.11, no IDesignTimeDbContextFactory - WORKS
Using project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
Using startup project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmpwotm5q.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmpg2k1nh.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
Build started...
dotnet build C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj /verbosity:quiet /nologo /p:PublishAot=false
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:06.17
Build succeeded.
dotnet exec --depsfile C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.deps.json --additionalprobingpath C:\FakeUserProfiles\richard.ward\nuget --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.runtimeconfig.json C:\FakeUserProfiles\richard.ward\nuget\dotnet-ef\9.0.0\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll database update --assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --startup-assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --startup-project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --project-dir C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ --root-namespace ReproTest --language C# --framework net9.0 --nullable --working-dir C:\FakeUserProfiles\richard.ward\repos\Test --verbose
Using assembly 'ReproTest'.
Using startup assembly 'ReproTest'.
Using application base 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0'.
Using working directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest'.
Using root namespace 'ReproTest'.
Using project directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'ReproTest'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'TestDbContext'.
Finding DbContext classes in the project...
Using context 'TestDbContext'.
Finding design-time services referenced by assembly 'ReproTest'...
Finding design-time services referenced by assembly 'ReproTest'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'ReproTest'...
No design-time services were found.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (23ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Executed DbCommand (23ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (18ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Executed DbCommand (18ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
info: Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
No migrations were applied. The database is already up to date.
Done.
EF Core 8.0.11, with IDesignTimeDbContextFactory - WORKS
Using project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
Using startup project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmpl42vyr.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmpyp4ywh.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
Build started...
dotnet build C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj /verbosity:quiet /nologo /p:PublishAot=false
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:05.10
Build succeeded.
dotnet exec --depsfile C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.deps.json --additionalprobingpath C:\FakeUserProfiles\richard.ward\nuget --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.runtimeconfig.json C:\FakeUserProfiles\richard.ward\nuget\dotnet-ef\9.0.0\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll database update --assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --startup-assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --startup-project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --project-dir C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ --root-namespace ReproTest --language C# --framework net9.0 --nullable --working-dir C:\FakeUserProfiles\richard.ward\repos\Test --verbose
Using assembly 'ReproTest'.
Using startup assembly 'ReproTest'.
Using application base 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0'.
Using working directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest'.
Using root namespace 'ReproTest'.
Using project directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'DesignTimeContextFactory'.
Found DbContext 'TestDbContext'.
Finding application service provider in assembly 'ReproTest'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Finding DbContext classes in the project...
Using DbContext factory 'DesignTimeContextFactory'.
Using context 'TestDbContext'.
Finding design-time services referenced by assembly 'ReproTest'...
Finding design-time services referenced by assembly 'ReproTest'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'ReproTest'...
No design-time services were found.
Creating DbConnection.
Created DbConnection. (50ms).
Migrating using database 'TestDb' on server '(localdb)\mssqllocaldb'.
Opening connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Opened connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Creating DbCommand for 'ExecuteNonQuery'.
Created DbCommand for 'ExecuteNonQuery' (9ms).
Initialized DbCommand for 'ExecuteNonQuery' (17ms).
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Executed DbCommand (29ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Closing connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Closed connection to database 'TestDb' on server '(localdb)\mssqllocaldb' (7ms).
Creating DbCommand for 'ExecuteScalar'.
Created DbCommand for 'ExecuteScalar' (2ms).
Initialized DbCommand for 'ExecuteScalar' (4ms).
Opening connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Opened connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Executed DbCommand (19ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Closing connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Closed connection to database 'TestDb' on server '(localdb)\mssqllocaldb' (2ms).
Opening connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Opened connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Creating DbCommand for 'ExecuteNonQuery'.
Created DbCommand for 'ExecuteNonQuery' (0ms).
Initialized DbCommand for 'ExecuteNonQuery' (1ms).
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Closing connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Closed connection to database 'TestDb' on server '(localdb)\mssqllocaldb' (0ms).
Creating DbCommand for 'ExecuteScalar'.
Created DbCommand for 'ExecuteScalar' (0ms).
Initialized DbCommand for 'ExecuteScalar' (1ms).
Opening connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Opened connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Closing connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Closed connection to database 'TestDb' on server '(localdb)\mssqllocaldb' (1ms).
Opening connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Opened connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Creating DbCommand for 'ExecuteReader'.
Created DbCommand for 'ExecuteReader' (0ms).
Initialized DbCommand for 'ExecuteReader' (1ms).
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Closing data reader to 'TestDb' on server '(localdb)\mssqllocaldb'.
A data reader for 'TestDb' on server '(localdb)\mssqllocaldb' is being disposed after spending 4ms reading results.
Closing connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Closed connection to database 'TestDb' on server '(localdb)\mssqllocaldb' (0ms).
No migrations were applied. The database is already up to date.
'TestDbContext' disposed.
Disposing connection to database 'TestDb' on server '(localdb)\mssqllocaldb'.
Disposed connection to database '' on server '' (1ms).
Done.
EF Core 9.0.0, no IDesignTimeDbContextFactory - WORKS
Using project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
Using startup project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmppuq5sx.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmprbhnb1.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
Build started...
dotnet build C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj /verbosity:quiet /nologo /p:PublishAot=false
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:06.05
Build succeeded.
dotnet exec --depsfile C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.deps.json --additionalprobingpath C:\FakeUserProfiles\richard.ward\nuget --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.runtimeconfig.json C:\FakeUserProfiles\richard.ward\nuget\dotnet-ef\9.0.0\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll database update --assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --startup-assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --startup-project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --project-dir C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ --root-namespace ReproTest --language C# --framework net9.0 --nullable --working-dir C:\FakeUserProfiles\richard.ward\repos\Test --verbose
Using assembly 'ReproTest'.
Using startup assembly 'ReproTest'.
Using application base 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0'.
Using working directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest'.
Using root namespace 'ReproTest'.
Using project directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding DbContext classes in the project...
Found DbContext 'TestDbContext'.
Finding application service provider in assembly 'ReproTest'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Using context 'TestDbContext'.
Finding design-time services referenced by assembly 'ReproTest'...
Finding design-time services referenced by assembly 'ReproTest'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'ReproTest'...
No design-time services were found.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (22ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Executed DbCommand (22ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
info: Microsoft.EntityFrameworkCore.Migrations[20411]
Acquiring an exclusive lock for migration application. See https://aka.ms/efcore-docs-migrations-lock for more information if this takes too long.
Acquiring an exclusive lock for migration application. See https://aka.ms/efcore-docs-migrations-lock for more information if this takes too long.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (24ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @result int;
EXEC @result = sp_getapplock @Resource = '__EFMigrationsLock', @LockOwner = 'Session', @LockMode = 'Exclusive';
SELECT @result
Executed DbCommand (24ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @result int;
EXEC @result = sp_getapplock @Resource = '__EFMigrationsLock', @LockOwner = 'Session', @LockMode = 'Exclusive';
SELECT @result
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL
BEGIN
CREATE TABLE [__EFMigrationsHistory] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
);
END;
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [MigrationId], [ProductVersion]
FROM [__EFMigrationsHistory]
ORDER BY [MigrationId];
No migrations were applied. The database is already up to date.
info: Microsoft.EntityFrameworkCore.Migrations[20405]
No migrations were applied. The database is already up to date.
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @result int;
EXEC @result = sp_releaseapplock @Resource = '__EFMigrationsLock', @LockOwner = 'Session';
SELECT @result
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
DECLARE @result int;
EXEC @result = sp_releaseapplock @Resource = '__EFMigrationsLock', @LockOwner = 'Session';
SELECT @result
Done.
EF Core 9.0.0, with IDesignTimeDbContextFactory - DOES NOT WORK
Using project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
Using startup project 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj'.
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmp2r1lqx.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\FakeUserProfiles\richard.ward\temp\tmpfn3pxx.tmp /verbosity:quiet /nologo C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj
Build started...
dotnet build C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj /verbosity:quiet /nologo /p:PublishAot=false
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:04.96
Build succeeded.
dotnet exec --depsfile C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.deps.json --additionalprobingpath C:\FakeUserProfiles\richard.ward\nuget --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.runtimeconfig.json C:\FakeUserProfiles\richard.ward\nuget\dotnet-ef\9.0.0\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll database update --assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --startup-assembly C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0\ReproTest.dll --startup-project C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ReproTest.csproj --project-dir C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\ --root-namespace ReproTest --language C# --framework net9.0 --nullable --working-dir C:\FakeUserProfiles\richard.ward\repos\Test --verbose
Using assembly 'ReproTest'.
Using startup assembly 'ReproTest'.
Using application base 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\bin\Debug\net9.0'.
Using working directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest'.
Using root namespace 'ReproTest'.
Using project directory 'C:\FakeUserProfiles\richard.ward\repos\Test\ReproTest\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'DesignTimeContextFactory'.
Found DbContext 'TestDbContext'.
Finding DbContext classes in the project...
Using DbContext factory 'DesignTimeContextFactory'.
Using context 'TestDbContext'.
Finding design-time services referenced by assembly 'ReproTest'...
Finding design-time services referenced by assembly 'ReproTest'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'ReproTest'...
No design-time services were found.
Creating DbConnection.
Created DbConnection. (18ms).
Migrating using database '' on server ''.
Opening connection to database '' on server ''.
An error occurred using the connection to database '' on server ''.
'TestDbContext' disposed.
Disposing connection to database '' on server ''.
Disposed connection to database '' on server '' (4ms).
System.InvalidOperationException: The ConnectionString property has not been initialized.
at Microsoft.Data.SqlClient.SqlConnection.PermissionDemand()
at Microsoft.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext _, TState s)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The ConnectionString property has not been initialized.
Running
dotnet ef database update
while using EF Core 8.0.11 or earlier used to set ASPNETCORE_ENVIRONMENT and DOTNET_ENVIRONMENT to "Development" if it was not explicitly specified. On updating to EF Core 9, those do not default if there is an implementation ofIDesignTimeDbContextFactory
. The version of dotnet-ef (at least between 9.0.0 and 8.0.11) does not seem to matter.I believe this is the code in question, though I haven't managed to chase down why it's not being hit for 9.0.0 with
IDesignTimeDbContextFactory
.This project can be manipulated to reproduce the issue, though I've also included some relevant tool output from
dotnet ef database update --project .\ReproTest\ --verbose
below.My current workaround is to explicitly set the Environment as described at https://learn.microsoft.com/en-us/ef/core/cli/dotnet#aspnet-core-environment .
Provider and version information
EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 8 and 9 Operating system: Windows 11 23H2, 22631.4460 IDE: Visual Studio 2022 17.12.0
Log output
EF Core 8.0.11, no IDesignTimeDbContextFactory - WORKS
EF Core 8.0.11, with IDesignTimeDbContextFactory - WORKS
EF Core 9.0.0, no IDesignTimeDbContextFactory - WORKS
EF Core 9.0.0, with IDesignTimeDbContextFactory - DOES NOT WORK