Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

efbundle fails in version 9.0.1: Could not load Microsoft.Extensions.Configuration.FileExtensions #35489

Closed
MagMar94 opened this issue Jan 17, 2025 · 2 comments
Assignees

Comments

@MagMar94
Copy link

MagMar94 commented Jan 17, 2025

Bug description

We run efbundle as part of our Azure Pipeline:

cd $(System.DefaultWorkingDirectory)/build-artifacts/folder/migrations
.\efbundle --connection "..."

This crashes with the following error:

2025-01-16T12:37:10.1736768Z ========================== Starting Command Output ===========================
2025-01-16T12:37:10.2116103Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\ac36f023-2e25-4f6a-aeb7-4b22caa2b034.ps1'"
2025-01-16T12:37:26.7789750Z Unable to create a 'DbContext' of type 'Namespace.Infrastructure.Database.DbContext.EsDbContext'. The exception 'Could not load file or assembly 'Microsoft.Extensions.Configuration.FileExtensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
2025-01-16T12:37:27.0535509Z ##[error]PowerShell exited with code '1'.

The only change we have made since the task started failing was updating ef tools from 9.0.0 to 9.0.1 and reflecting this in the ModelSnapshot with HasAnnotation("ProductVersion", "9.0.1").

The NuGet-packages was updated from 9.0.0 to 9.0.1 in the previous commit, but the tools were forgotten and had to be pushed separately. The first release with only NuGet updates did not have any problems when running efbundle, but the second release with the updated tools started failing.

Your code

The only difference between the failing execution and the previous one is a change to dotnet-tools.json and the model snapshot:

OLD dotnet-tools.json that worked:

 "dotnet-ef": {
      "version": "9.0.0",
      "commands": [
        "dotnet-ef"
      ],
      "rollForward": false
    }

NEW dotnet-tools.json in version with error:

 "dotnet-ef": {
      "version": "9.0.1",
      "commands": [
        "dotnet-ef"
      ],
      "rollForward": false
    }

OLD snapshot that worked:

modelBuilder
                .HasAnnotation("ProductVersion", "9.0.0")
                .HasAnnotation("Relational:MaxIdentifierLength", 128);

NEW snapshot in version with error:

modelBuilder
                .HasAnnotation("ProductVersion", "9.0.1")
                .HasAnnotation("Relational:MaxIdentifierLength", 128);

The code we run in the pipeline is this PowerShell Task:

cd $(System.DefaultWorkingDirectory)/build-artifacts/folder/migrations
.\efbundle --connection "Data Source=...;Initial Catalog=...; Authentication=Active Directory Service Principal;Encrypt=False; User Id=...; Password=$(ServicePrincipalSecret)"

In the commit before the migration started failing we updated the following NuGet-packages from 9.0.0 to 9.0.1:

  • Microsoft.EntityFrameworkCore.Abstractions
  • Microsoft.Extensions.Configuration.Abstractions
  • Microsoft.Extensions.Logging.Abstractions
  • System.Text.Json
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.Extensions.Configuration.Binder
  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Hosting.Abstractions
  • Microsoft.Extensions.Http
  • Microsoft.Extensions.Options.ConfigurationExtensions
  • System.Configuration.ConfigurationManager
  • Microsoft.AspNetCore.AzureAppServices.HostingStartup
  • Microsoft.Extensions.Configuration.UserSecrets
  • Microsoft.AspNetCore.SpaServices.Extensions
    We also updated Microsoft.Data.SqlClient from 5.2.1 to 5.2.2.

After the update the release pipeline worked fine and the migrations were run. When we realized we had forgotten updating the tools version together with the NuGet-packages we updated them, and then the migrations started failing.

Stack traces

2025-01-16T12:30:46.4709507Z ##[section]Starting: Migrate UAT staging database
2025-01-16T12:30:46.4716052Z ==============================================================================
2025-01-16T12:30:46.4716187Z Task         : PowerShell
2025-01-16T12:30:46.4716273Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2025-01-16T12:30:46.4716378Z Version      : 2.247.1
2025-01-16T12:30:46.4716445Z Author       : Microsoft Corporation
2025-01-16T12:30:46.4716556Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2025-01-16T12:30:46.4716664Z ==============================================================================
2025-01-16T12:30:48.1610679Z Generating script.
2025-01-16T12:30:48.3280709Z ========================== Starting Command Output ===========================
2025-01-16T12:30:48.4341419Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\6e8be368-8aab-47fd-af9c-a93915c08cc0.ps1'"
2025-01-16T12:30:59.1537731Z Unable to create a 'DbContext' of type 'Namespace.Infrastructure.Database.DbContext.EsDbContext'. The exception 'Could not load file or assembly 'Microsoft.Extensions.Configuration.FileExtensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
2025-01-16T12:30:59.3481557Z ##[error]PowerShell exited with code '1'.
2025-01-16T12:30:59.3873099Z ##[section]Finishing: Migrate UAT staging database

Verbose output

2025-01-16T14:30:11.8626832Z ##[section]Starting: Migrate UAT staging database
2025-01-16T14:30:11.8635458Z ==============================================================================
2025-01-16T14:30:11.8635635Z Task         : PowerShell
2025-01-16T14:30:11.8635733Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2025-01-16T14:30:11.8635860Z Version      : 2.247.1
2025-01-16T14:30:11.8635941Z Author       : Microsoft Corporation
2025-01-16T14:30:11.8636067Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2025-01-16T14:30:11.8636195Z ==============================================================================
2025-01-16T14:30:13.5534011Z Generating script.
2025-01-16T14:30:13.6815451Z ========================== Starting Command Output ===========================
2025-01-16T14:30:13.8155027Z ##[command]"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\81d119c8-7091-4a14-8091-a0ae3a62e123.ps1'"
2025-01-16T14:30:32.7171552Z Finding DbContext classes...
2025-01-16T14:30:32.7179146Z Using environment 'Development'.
2025-01-16T14:30:32.7187213Z Finding IDesignTimeDbContextFactory implementations...
2025-01-16T14:30:35.8268690Z Found IDesignTimeDbContextFactory implementation 'EsDbContextFactory'.
2025-01-16T14:30:35.8513525Z Found DbContext 'EsDbContext'.
2025-01-16T14:30:35.8523711Z Finding DbContext classes in the project...
2025-01-16T14:30:36.6517547Z Using DbContext factory 'EsDbContextFactory'.
2025-01-16T14:30:40.2833453Z Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create a 'DbContext' of type 'Namespace.Infrastructure.Database.DbContext.EsDbContext'. The exception 'Could not load file or assembly 'Microsoft.Extensions.Configuration.FileExtensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
2025-01-16T14:30:40.2835912Z  ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Extensions.Configuration.FileExtensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
2025-01-16T14:30:40.2837479Z File name: 'Microsoft.Extensions.Configuration.FileExtensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
2025-01-16T14:30:40.2838230Z    at Namespace.Infrastructure.Database.DbContext.EsDbContextFactory.CreateDbContext(String[] args)
2025-01-16T14:30:40.2838874Z    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2025-01-16T14:30:40.2839553Z    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
2025-01-16T14:30:40.2840127Z    --- End of inner exception stack trace ---
2025-01-16T14:30:40.2840715Z    at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType, KeyValuePair`2 contextPair)
2025-01-16T14:30:40.2841694Z    at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
2025-01-16T14:30:40.2843173Z    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
2025-01-16T14:30:40.2846740Z    at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.ExecuteInternal(String[] args)
2025-01-16T14:30:40.2847447Z    at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.<>c__DisplayClass6_0.<Configure>b__0(String[] args)
2025-01-16T14:30:40.2848295Z    at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
2025-01-16T14:30:40.2854266Z    at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsBundle.Execute(String context, Assembly assembly, Assembly startupAssembly, String[] args)
2025-01-16T14:30:40.2858584Z Unable to create a 'DbContext' of type 'Namespace.Infrastructure.Database.DbContext.EsDbContext'. The exception 'Could not load file or assembly 'Microsoft.Extensions.Configuration.FileExtensions, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.' was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
2025-01-16T14:30:40.5511403Z ##[error]PowerShell exited with code '1'.
2025-01-16T14:30:40.6023072Z ##[section]Finishing: Migrate UAT staging database

EF Core version

9.0.1

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET9.0

Operating system

Azure Agent: windows-latest

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jan 18, 2025

Is Microsoft.EntityFrameworkCore.Design referenced with PrivateAssets = all? If so, try changing the reference to this:

    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <Publish>true</Publish>
    </PackageReference>

If that doesn't help try adding this to your EsDbContext project:

<PropertyGroup>
   <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

@AndriySvyryd AndriySvyryd self-assigned this Jan 18, 2025
@MagMar94
Copy link
Author

By default, the package installs with PrivateAssets="All" and IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive", but adding <Publish>true</Publish> to the package reference worked 👍

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants