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
相关产品推荐
相关产品推荐

