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

WinUI3引入EF Core.SqlServer后CoreWebView2WebErrorStatus类型冲突求助

解决WinUI3 + EntityFrameworkCore.SqlServer 类型冲突问题

方案1:显式指定程序集别名

  • 更新Microsoft.Web.WebView2到最新稳定版:右键项目→管理NuGet程序包,找到该包并更新,新版本通常会修复这类命名冲突问题
  • 编辑项目的.csproj文件,添加程序集别名配置:
<ItemGroup>
  <Reference Include="Microsoft.Web.WebView2.Core">
    <Aliases>WebView2Core</Aliases>
  </Reference>
</ItemGroup>
  • 在代码中通过别名指定要引用的类型:
extern alias WebView2Core;
// 使用时明确指定别名下的命名空间
var errorStatus = WebView2Core::Microsoft.Web.WebView2.Core.CoreWebView2WebErrorStatus.ConnectionAborted;

方案2:统一依赖版本,移除冗余引用

  • 检查项目依赖树,若WinUI3已内置WebView2相关组件,可移除项目中直接添加的Microsoft.Web.WebView2引用
  • 在.csproj中配置集中版本管理,强制所有依赖使用统一的WebView2版本:
<PropertyGroup>
  <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
  <PackageVersion Include="Microsoft.Web.WebView2" Version="1.0.2526.47" /> <!-- 替换为当前最新稳定版 -->
</ItemGroup>

方案3:配置程序集版本重定向

针对桌面WinUI3项目,可在app.config中添加重定向规则,强制使用指定版本的程序集:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Web.WebView2.Core" publicKeyToken="2a8ab48044d2601e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.864.35" newVersion="1.0.2526.47" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

若为打包后的WinUI3应用,可在runtimeconfig.json中添加对应配置:

{
  "runtimeOptions": {
    "assemblyBinding": {
      "dependentAssembly": [
        {
          "assemblyIdentity": {
            "name": "Microsoft.Web.WebView2.Core",
            "publicKeyToken": "2a8ab48044d2601e"
          },
          "bindingRedirect": {
            "oldVersion": "0.0.0.0-1.0.864.35",
            "newVersion": "1.0.2526.47"
          }
        }
      ]
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 08:37:16