You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

efbundle执行报错:无法加载Microsoft.Extensions.Configuration.FileExtensions

问题:EF Migrations Bundle执行时缺失配置类库

项目背景

  • 基于.NET 7构建,包含4个项目:Core、DataAccess(DbContext所在项目)、Service、UI,本地运行正常
  • 采用Code First模式,使用7.0.4版本的dotnet ef手动执行数据库迁移无异常
  • 通过DesignTimeDbContextFactory从UI层的appsettings.json读取连接字符串,工厂核心代码如下:
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");
    // DbContext初始化逻辑
...

问题现象

使用dotnet ef migrations bundle -f -v --self-contained生成efbundle.exe后,执行该文件时抛出以下错误:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---&gt; 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)
...

已尝试操作

生成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

执行bundle的方式

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

调试与解决建议

  • 补全DataAccess项目的依赖引用:确保DataAccess项目直接安装Microsoft.Extensions.Configuration.FileExtensions和Microsoft.Extensions.Configuration.Json NuGet包(版本选用7.0.0或与.NET 7.0.4兼容的版本)。因为bundle打包时,设计时工厂的依赖不会自动从UI项目传递过来。
  • 优化配置读取逻辑:在DesignTimeDbContextFactory中优先读取命令行传入的连接字符串,跳过配置文件读取(当执行efbundle.exe --connection时),同时简化路径硬编码:
    public MyContext CreateDbContext(string[] args)
    {
        // 优先获取命令行传入的连接字符串
        var connectionString = args.FirstOrDefault(a => a.StartsWith("--connection="))?.Split('=')[1];
        
        if (string.IsNullOrEmpty(connectionString))
        {
            // 从配置文件读取的逻辑,调整路径为相对路径避免硬编码
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "<UI项目相对路径>"))
                .AddJsonFile("appsettings.json")
                .Build();
            connectionString = configuration.GetConnectionString("MyAppConnection");
        }
    
        var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
        optionsBuilder.UseSqlServer(connectionString);
        return new MyContext(optionsBuilder.Options);
    }
    
  • 指定目标运行时生成bundle:生成bundle时添加--runtime参数明确指定目标环境(例如win-x64),避免跨平台依赖缺失:
    dotnet ef migrations bundle --self-contained --runtime win-x64 --startup-project "<Path To UI Project>\API.csproj" -f -v
    
  • 清理重建bundle:删除已生成的efbundle.exe,清空项目的bin/obj目录后重新执行bundle生成命令,确保依赖项被正确打包。
  • 验证bundle依赖完整性:使用dotnet list efbundle.exe package或ILSpy等工具检查bundle中是否包含Microsoft.Extensions.Configuration.FileExtensions程序集,确认是否在打包时被遗漏。

内容的提问来源于stack exchange,提问作者Naveen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 15:34:57