Hi Folks,
I am building a WinUI 3 app and want to use SQLite using ef core. When using the Microsoft.Extensions.Hosting package, the dotnet ef migrations add command doesn't give any errors but also doesn't give or log any errors. When removed from the .csproj file the and use Microsoft.Extensions.DependencyInjection, the migration is created without any problems.
I created a small sample 3 app with ef core. In the current state of the project within App1.zip is Microsoft.Extensions.Hosting included.
Am I missing something?
How to reproduce?
- Open project, :)
- Run
dotnet ef migrations add InitialCreate -v
- Watch it do nothing
- Remove Microsoft.Extensions.Hosting in the .csproj
- Run
dotnet clean
- Run
dotnet ef migrations add InitialCreate -v again
- Watch it create a migration
Code
Project zip
App1.zip
The constructor in the App.xaml.cs.
public App()
{
this.InitializeComponent();
var serviceProvider = new ServiceCollection().AddDbContext<AppDbContext>(ServiceLifetime.Transient).BuildServiceProvider();
//var host = new HostBuilder()
// .ConfigureServices(services =>
// services.AddDbContext<AppDbContext>(ServiceLifetime.Transient))
// .Build();
//host.Start();
}
Context factory
internal class TestDatabaseContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
var DbPath = System.IO.Path.Join(path, "blogging.db");
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseSqlite($"Data Source={DbPath}");
return new AppDbContext(optionsBuilder.Options);
}
}
Output
Using Microsoft.Extensions.DependencyInjection
PS C:\Users\User\source\repos\App1\App1> dotnet ef migrations add InitialCreate -v
Using project 'C:\Users\User\source\repos\App1\App1\App1.csproj'.
Using startup project 'C:\Users\User\source\repos\App1\App1\App1.csproj'.
Writing 'C:\Users\User\source\repos\App1\App1\obj\App1.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\User\AppData\Local\Temp\tmpp110d3.tmp /verbosity:quiet /nologo C:\Users\User\source\repos\App1\App1\App1.csproj
Writing 'C:\Users\User\source\repos\App1\App1\obj\App1.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\User\AppData\Local\Temp\tmp5psv0t.tmp /verbosity:quiet /nologo C:\Users\User\source\repos\App1\App1\App1.csproj
Build started...
dotnet build C:\Users\User\source\repos\App1\App1\App1.csproj /verbosity:quiet /nologo /p:PublishAot=false
c:\program files\dotnet\sdk\8.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(284,5): warning NETSDK1206: Found version-specific or distribution-specific runtime identifier(s): win10-arm64, win10-x64, win10-x86. Affected libraries: Microsoft.WindowsAppSDK. In .NET 8.0 and higher, assets for version-specific and distribution-specific runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details. [C:\Users\User\source\repos\App1\App1\App1.csproj]
Build succeeded.
c:\program files\dotnet\sdk\8.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(284,5): warning NETSDK1206: Found version-specific or distribution-specific runtime identifier(s): win10-arm64, win10-x64, win10-x86. Affected libraries: Microsoft.WindowsAppSDK. In .NET 8.0 and higher, assets for version-specific and distribution-specific runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details. [C:\Users\User\source\repos\App1\App1\App1.csproj]
1 Warning(s)
0 Error(s)
Time Elapsed 00:00:14.96
Build succeeded.
dotnet exec --depsfile C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.deps.json --additionalprobingpath C:\Users\User\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --additionalprobingpath "c:\program files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.runtimeconfig.json C:\Users\User\.dotnet\tools\.store\dotnet-ef\8.0.1\dotnet-ef\8.0.1\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll migrations add InitialCreate --assembly C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.dll --project C:\Users\User\source\repos\App1\App1\App1.csproj --startup-assembly C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.dll --startup-project C:\Users\User\source\repos\App1\App1\App1.csproj --project-dir C:\Users\User\source\repos\App1\App1\ --root-namespace App1 --language C# --framework net8.0-windows10.0.19041.0 --working-dir C:\Users\User\source\repos\App1\App1 --verbose
Using assembly 'App1'.
Using startup assembly 'App1'.
Using application base 'C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0'.
Using working directory 'C:\Users\User\source\repos\App1\App1'.
Using root namespace 'App1'.
Using project directory 'C:\Users\User\source\repos\App1\App1\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'TestDatabaseContextFactory'.
Found DbContext 'AppDbContext'.
Finding application service provider in assembly 'App1'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Using DbContext factory 'TestDatabaseContextFactory'.
Using context 'AppDbContext'.
Finding design-time services referenced by assembly 'App1'...
Finding design-time services referenced by assembly 'App1'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.Sqlite'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.Sqlite'.
Finding IDesignTimeServices implementations in assembly 'App1'...
No design-time services were found.
Writing migration to 'C:\Users\User\source\repos\App1\App1\Migrations\20240115212727_InitialCreate.cs'.
Writing model snapshot to 'C:\Users\User\source\repos\App1\App1\Migrations\AppDbContextModelSnapshot.cs'.
'AppDbContext' disposed.
Done. To undo this action, use 'ef migrations remove'
PS C:\Users\User\source\repos\App1\App1>
Using Microsoft.Extensions.Hosting
PS C:\Users\User\source\repos\App1\App1> dotnet ef migrations add InitialCreate -v
Using project 'C:\Users\User\source\repos\App1\App1\App1.csproj'.
Using startup project 'C:\Users\User\source\repos\App1\App1\App1.csproj'.
Writing 'C:\Users\User\source\repos\App1\App1\obj\App1.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\User\AppData\Local\Temp\tmp3zndbe.tmp /verbosity:quiet /nologo C:\Users\User\source\repos\App1\App1\App1.csproj
Writing 'C:\Users\User\source\repos\App1\App1\obj\App1.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\User\AppData\Local\Temp\tmpz5aegy.tmp /verbosity:quiet /nologo C:\Users\User\source\repos\App1\App1\App1.csproj
Build started...
dotnet build C:\Users\User\source\repos\App1\App1\App1.csproj /verbosity:quiet /nologo /p:PublishAot=false
c:\program files\dotnet\sdk\8.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(284,5): warning NETSDK1206: Found version-specific or distribution-specific runtime identifier(s): win10-arm64, win10-x64, win10-x86. Affected libraries: Microsoft.WindowsAppSDK. In .NET 8.0 and higher, assets for version-specific and distribution-specific runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details. [C:\Users\User\source\repos\App1\App1\App1.csproj]
Build succeeded.
c:\program files\dotnet\sdk\8.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(284,5): warning NETSDK1206: Found version-specific or distribution-specific runtime identifier(s): win10-arm64, win10-x64, win10-x86. Affected libraries: Microsoft.WindowsAppSDK. In .NET 8.0 and higher, assets for version-specific and distribution-specific runtime identifiers will not be found by default. See https://aka.ms/dotnet/rid-usage for details. [C:\Users\User\source\repos\App1\App1\App1.csproj]
1 Warning(s)
0 Error(s)
Time Elapsed 00:00:08.86
Build succeeded.
dotnet exec --depsfile C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.deps.json --additionalprobingpath C:\Users\User\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --additionalprobingpath "c:\program files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.runtimeconfig.json C:\Users\User\.dotnet\tools\.store\dotnet-ef\8.0.1\dotnet-ef\8.0.1\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll migrations add InitialCreate --assembly C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.dll --project C:\Users\User\source\repos\App1\App1\App1.csproj --startup-assembly C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0\App1.dll --startup-project C:\Users\User\source\repos\App1\App1\App1.csproj --project-dir C:\Users\User\source\repos\App1\App1\ --root-namespace App1 --language C# --framework net8.0-windows10.0.19041.0 --working-dir C:\Users\User\source\repos\App1\App1 --verbose
Using assembly 'App1'.
Using startup assembly 'App1'.
Using application base 'C:\Users\User\source\repos\App1\App1\bin\Debug\net8.0-windows10.0.19041.0'.
Using working directory 'C:\Users\User\source\repos\App1\App1'.
Using root namespace 'App1'.
Using project directory 'C:\Users\User\source\repos\App1\App1\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'TestDatabaseContextFactory'.
Found DbContext 'AppDbContext'.
Finding application service provider in assembly 'App1'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
PS C:\Users\User\source\repos\App1\App1>
Include provider and version information
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SQLite (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: .NET 8
Operating system: Windows 11
IDE: Visual Studio 2022 17.9 Preview
Hi Folks,
I am building a WinUI 3 app and want to use SQLite using ef core. When using the Microsoft.Extensions.Hosting package, the
dotnet ef migrations addcommand doesn't give any errors but also doesn't give or log any errors. When removed from the .csproj file the and use Microsoft.Extensions.DependencyInjection, the migration is created without any problems.I created a small sample 3 app with ef core. In the current state of the project within App1.zip is Microsoft.Extensions.Hosting included.
Am I missing something?
How to reproduce?
dotnet ef migrations add InitialCreate -vdotnet cleandotnet ef migrations add InitialCreate -vagainCode
Project zip
App1.zip
The constructor in the App.xaml.cs.
Context factory
Output
Using Microsoft.Extensions.DependencyInjection
Using Microsoft.Extensions.Hosting
Include provider and version information
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SQLite (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: .NET 8
Operating system: Windows 11
IDE: Visual Studio 2022 17.9 Preview