serilog-mssql / serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
Apache License 2.0
283 stars 148 forks source link

After upgrading to 6.4.0, running dotnet ef migrations script command cannot create migration script #499

Closed philippedurocher closed 11 months ago

philippedurocher commented 11 months ago

Everything is working with 6.3.0 but after upgrading to 6.4.0 it's not working.

I'm using dotnet 8.0.100, EF tools 8.0.0, Serilog.AspNetCore 8.0.0

The command :

PM> dotnet ef migrations script --output .\sql\dbscript.sql --idempotent --project .\src\Oscar\Oscar.csproj --context ApplicationDbContext --verbose

Here's the error log

PM> dotnet ef migrations script --output .\sql\dbscript.sql --idempotent --project .\src\Oscar\Oscar.csproj --context ApplicationDbContext --verbose
Using project 'C:\Dev\Oscar\src\Oscar\Oscar.csproj'.
Using startup project 'C:\Dev\Oscar\src\Oscar\Oscar.csproj'.
Writing 'C:\Dev\Oscar\src\Oscar\obj\Oscar.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\durphi01\AppData\Local\Temp\tmpwcq5t4.tmp /verbosity:quiet /nologo C:\Dev\Oscar\src\Oscar\Oscar.csproj
Writing 'C:\Dev\Oscar\src\Oscar\obj\Oscar.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\durphi01\AppData\Local\Temp\tmplbettc.tmp /verbosity:quiet /nologo C:\Dev\Oscar\src\Oscar\Oscar.csproj
Build started...
dotnet build C:\Dev\Oscar\src\Oscar\Oscar.csproj /verbosity:quiet /nologo /p:PublishAot=false

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:15.68
Build succeeded.
dotnet exec --depsfile C:\Dev\Oscar\src\Oscar\bin\Debug\net8.0\Oscar.deps.json --additionalprobingpath c:\dev\.nuget\packages --runtimeconfig C:\Dev\Oscar\src\Oscar\bin\Debug\net8.0\Oscar.runtimeconfig.json C:\Users\durphi01\.dotnet\tools\.store\dotnet-ef\8.0.0\dotnet-ef\8.0.0\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll migrations script --output .\sql\dbscript.sql --idempotent --context ApplicationDbContext --assembly C:\Dev\Oscar\src\Oscar\bin\Debug\net8.0\Oscar.dll --project C:\Dev\Oscar\src\Oscar\Oscar.csproj --startup-assembly C:\Dev\Oscar\src\Oscar\bin\Debug\net8.0\Oscar.dll --startup-project C:\Dev\Oscar\src\Oscar\Oscar.csproj --project-dir C:\Dev\Oscar\src\Oscar\ --root-namespace Oscar --language C# --framework net8.0 --nullable --working-dir C:\Dev\Oscar --verbose
Using assembly 'Oscar'.
Using startup assembly 'Oscar'.
Using application base 'C:\Dev\Oscar\src\Oscar\bin\Debug\net8.0'.
Using working directory 'C:\Dev\Oscar\src\Oscar'.
Using root namespace 'Oscar'.
Using project directory 'C:\Dev\Oscar\src\Oscar\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Oscar'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
System.InvalidOperationException: The entry point exited without ever building an IHost.
   at Microsoft.Extensions.Hosting.HostFactoryResolver.HostingListener.CreateHost()
   at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass10_0.<ResolveHostFactory>b__0(String[] args)
   at Microsoft.Extensions.Hosting.HostFactoryResolver.<>c__DisplayClass13_0.<ResolveServiceProviderFactory>b__3(String[] args)
   at Microsoft.EntityFrameworkCore.Design.Internal.AppServiceProviderFactory.CreateFromHosting(String[] args)
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: The entry point exited without ever building an IHost.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'ApplicationDbContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Oscar.Infrastructure.Data.ApplicationDbContext]' while attempting to activate 'Oscar.Infrastructure.Data.ApplicationDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
 ---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Oscar.Infrastructure.Data.ApplicationDbContext]' while attempting to activate 'Oscar.Infrastructure.Data.ApplicationDbContext'.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass20_5.<FindContextTypes>b__13()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.ScriptMigration(String fromMigration, String toMigration, MigrationsSqlGenerationOptions options, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigrationImpl(String fromMigration, String toMigration, Boolean idempotent, Boolean noTransactions, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create a 'DbContext' of type 'ApplicationDbContext'. The exception 'Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Oscar.Infrastructure.Data.ApplicationDbContext]' while attempting to activate 'Oscar.Infrastructure.Data.ApplicationDbContext'.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
PM> 

The code to register ApplicationDbContext image

The code to register Serilog image

The configuration image

ckadluba commented 11 months ago

Hi @philippedurocher!

Thank you for reporting this. I will investigate the issue.

Can you possibly provide a complete sample project to reproduce this?

Thank you!

philippedurocher commented 11 months ago

I have never been able to reproduce the problem on my machine. In fact, the issue occurs in my deployment pipeline when I try to extract the database migration script.

What I did to work around the problem is to implement IDesignTimeDbContextFactory for my DbContext, and at that point, my migration script was able to extract.

I have no idea why I didn't need this with 6.3.0 and now I need it with 6.4.0.

Sorry, I would like to provide more information, but I can't reproduce it locally, and I can't debug the pipeline.

ckadluba commented 11 months ago

No worries. Thanks for the update.

Should it occurr again, please let us know.