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

.NET7中使用FluentMigrator执行迁移时出现程序集绑定错误

问题描述

在.NET7环境中使用FluentMigrator执行数据库迁移,已通过dotnet tool install -g FluentMigrator.DotNet.Cli安装CLI工具,执行命令dotnet fm migrate -a Conversations.API.dll -p postgres -c "omitted"时,抛出System.IO.FileNotFoundException,提示无法加载System.Runtime, Version=7.0.0.0程序集。添加--allowDirtyAssemblies参数后,又出现System.TypeLoadException,提示无法从Google.Protobuf, Version=3.5.1.0程序集中加载Google.Protobuf.IBufferMessage类型。

项目的.csproj配置如下:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Dapper" Version="2.0.151" />
        <PackageReference Include="FluentMigrator" Version="3.3.2" />
        <PackageReference Include="FluentMigrator.Console" Version="3.3.2" />
        <PackageReference Include="FluentMigrator.Runner.Postgres" Version="3.3.2" />
        <PackageReference Include="Grpc.AspNetCore" Version="2.55.0" />
        <PackageReference Include="Npgsql" Version="7.0.4" />
        <PackageReference Include="PasetoBearer.Authentication" Version="2.0.0" />
        <PackageReference Include="StackExchange.Redis" Version="2.6.122" />
    </ItemGroup>

    <ItemGroup>
        <Protobuf Include="Protos\get_conversations.proto" GrpcServices="Server" />
    </ItemGroup>

    <ItemGroup>
        <Protobuf Include="Protos\get_conversation_messages.proto" GrpcServices="Server" />
    </ItemGroup>

</Project>
问题原因分析
  • FluentMigrator版本与.NET7兼容性不足:使用的FluentMigrator 3.3.2版本发布于.NET7正式版推出前,其CLI工具未针对.NET7的程序集加载逻辑做适配,无法正确识别.NET7的System.Runtime程序集版本,触发FileNotFoundException。
  • Google.Protobuf版本依赖冲突:Grpc.AspNetCore 2.55.0依赖的Google.Protobuf版本远高于3.5.1.0(该版本Grpc实际依赖3.21.x及以上版本的Google.Protobuf),但FluentMigrator 3.3.2内部引用了旧版本的Google.Protobuf。添加--allowDirtyAssemblies绕过版本检查后,旧版本程序集被加载,而新版本中IBufferMessage类型的定义或位置已变更,导致无法加载该类型,触发TypeLoadException。
  • 迁移依赖Web项目程序集的不合理性:直接使用Web项目的Conversations.API.dll作为迁移程序集,Web项目包含大量与迁移无关的依赖(如Grpc、Redis认证等),额外依赖引入了更多版本冲突的可能性,加剧了程序集加载问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 08:52:36