我为.NET8创建了一个空白的webapi项目,安装两个包Microsoft.Extensions.DependencyModel、Microsoft.Data.SqlClient 以下为源码:
using Microsoft.Extensions.DependencyModel; using System.Reflection; using System.Runtime.Loader; var builder = WebApplication.CreateBuilder(args); var list = DependencyContext.Default.RuntimeLibraries.ToList(); var assemblies = list.Select(m =>{ try { return AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(m.Name)); }catch { return null; } }).Where(m => m != null).ToList(); foreach (var assembly in assemblies) {foreach (var type in assembly.GetTypes()) { } } app.Run();
编译后会提示如标题的错误信息。 这在.NET6中不存在。 将 Microsoft.Data.SqlClient 更改为 System.Data.SqlClient 时会出现类似的错误 怎么解决呢?非常感谢。
这是由于 Microsoft.Data.SqlClient 中的错误造成的,正如here所述,目前的解决方法似乎是将 Microsoft.Data.SqlClient NuGet 包更新到最新的预览版本,目前为 v5.2.0-预览3.23201.1。 我遇到了同样的问题,并且可以确认此解决方法有效。
我对 Microsoft.EntityFrameworkCore.SqlServer 包也有同样的问题。 这是由 Microsoft.Data.SqlClient 包中的错误引起的:https://github.com/dotnet/SqlClient/issues/1930,它是随 SqlServer 隐式安装的。 Bug 在 5.2.0 版本中修复,但它不是 LTS,因此 EFCore 不会使用它们 https://github.com/dotnet/efcore/issues/33430
所以我们有两个选择: