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

EF Core 6.0执行dotnet ef migrations add InitialCreate迁移失败如何解决

报错详情

构建已启动...
构建成功。
System.IO.FileLoadException: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type, ObjectHandleOnStack keepAlive)
at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language, Boolean nullable, String[] remainingArguments)
at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor(String[] remainingArguments)
at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute(String[] args)
at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

排查解决步骤
  • 检查本地.NET SDK版本匹配度
    运行dotnet --list-sdks命令,确认本地已安装6.0.x版本的.NET SDK,若未安装需先安装对应版本,禁止使用7.0及以上版本SDK编译.NET 6项目,避免程序集引用版本错位。
  • 对齐EF Core工具版本
    运行dotnet ef --version检查全局dotnet-ef工具版本,确保大版本与项目引用的EF Core包版本一致,均为6.0.x。如果版本过高可先卸载原有工具:
    dotnet tool uninstall --global dotnet-ef
    再安装对应版本工具:
    dotnet tool install --global dotnet-ef --version 6.0.*
  • 统一项目依赖包版本
    打开项目.csproj文件,确认以下配置符合要求:
    1. 目标框架为net6.0
    2. 所有Microsoft.EntityFrameworkCore开头的依赖包(包括Design、对应数据库的Provider包)版本统一为6.0.x,无跨大版本混合引用情况
      参考配置示例:
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.25" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.25">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.25" />
      </ItemGroup>
    </Project>
    
  • 清理项目缓存重试
    删除项目根目录下的bin、obj文件夹,依次执行dotnet clean、dotnet restore命令重新清理还原依赖,之后再尝试运行迁移命令。
  • 多项目结构指定参数
    如果你的解决方案包含多个项目,执行迁移命令时需明确指定数据层项目和启动项目,避免工具默认加载错误的程序集,示例命令:
    dotnet ef migrations add InitialCreate --project 你的数据仓储类库项目名 --startup-project 你的Web/控制台启动项目名

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 22:54:05