Search code examples
c#.netentity-frameworknuget-packageentity-framework-migrations

Executing efbundle (post dotnet ef migrations budle) not able to load Microsoft.Extensions.Configuration.FileExtensions file/nuget


My solution has different projects (all dotnet 7):

  1. Core
  2. DataAccess (DbContext source project)
  3. Service
  4. UI

I am able to run my solution just fine.

I am using code first approach for Db migrations using dotnet ef version 7.0.4

The connection string comes from UI layer using Microsoft.Extensions.DependencyInjection. For migration steps the connection string is directly read from appsettings.json from UI layer as follows:

        public MyContext CreateDbContext(string[] args)
        {
            IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).
                AddJsonFile(@Directory.GetCurrentDirectory() + ".<UI project path>.//appsettings.json").Build();

            var connectionString = configuration.GetConnectionString("MyAppConnection");
...

Everything works fine when I am doing migrations manually and update database using dotnet ef cli commands.

I started working with dotnet ef migrations bundles for migrations from relese pipelines. When I create bundle using dotnet ef migrations bundle -f -v --self-contained the efbundle.exe is generated

But when executing the efbudle.exe, there is an error as follows:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.IO.FileNotFoundException:
File name: 'Microsoft.Extensions.Configuration.FileExtensions, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
   at DataAccess.Dbcontexts.DesignTimeDbContextFactory.CreateDbContext(String[] args)
...

I've tried using different options for generating bundle:

1)  dotnet ef migrations bundle -f -v
2)  dotnet ef migrations bundle --self-contained --startup-project "<Path To UI Project>\API.csproj" -f -v
3)  dotnet ef migrations bundle --self-contained --context MyContext --project "<Path To DataAccess Project>\dataAccess.csproj" --startup-project "<Path To UI Project>\API.csproj" -f -v

And executing the efbundle:

.\efbundle.exe --connection 'MyConnectionString'
.\efbundle.exe

Everytime its Configuration nuget file not able to load.

Please suggest how to debug/resolve this error.


Solution

  • It was a package dependency problem (and not efcore problem) as some dependencies were not loading due to PrivateAssets property selectively enabled for the dependency: Microsoft.EntityFrameworkCore.Design.

    Changing the property PrivateAssets to none in the DataAccess.csproj file resolved the problem and now the efbundle.exe works as expected.

    After the change the PackageReference entry of Microsoft.EntityFrameworkCore.Design looks like this:

    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
        <PrivateAssets>none</PrivateAssets>
    </PackageReference>
    

    Also, the appsettings.json file is also needed with the efbundle.exe to work (same folder).