我使用了数据库优先的方法。 该模型是正确的(或者至少看起来是这样) 但我总是收到这个错误。拜托,我已经尝试了很多事情.. 我的程序的完整代码(甚至是我创建数据库的 sql 脚本) 在这里:https://github.com/AntonioParroni/test-task-for-backend-stack/blob/main/Server/Models/ApplicationContext.cs
因为我有一台Mac。我使用 dotnet ef cli 命令(dbcontext 脚手架)创建了模型 我可以使用我的上下文。但我无法触及任何 DbSet..
public static void Main(string[] args)
{
using (ApplicationContext context = new ApplicationContext())
{
Console.WriteLine(context.Database.CanConnect());
var months = context.Months.ToList();
foreach (var month in months)
{
Console.WriteLine(month.MonthName);
}
}
//CreateHostBuilder(args).Build().Run();
}
这不是我第一次使用EF。之前,在许多简单的项目或任务中,一切都运行良好。在这里......我做什么并不重要(我什至尝试重命名所有列名称,删除除一个表之外的所有表,修改上下文代码,在一个新的、完全空的上使用此项目中的相同步骤项目..) 总是这样..
Unhandled exception. System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor' threw an exception.
---> System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.Query.QueryableMethods' threw an exception.
---> System.InvalidOperationException: Sequence contains more than one matching element
at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException() in System.Linq.dll:token 0x600041c+0xa
ect....
这是我的包参考文件
"restore":{"projectUniqueName":"/Users/mac/Documents/GitHub/test-task-for-backend-stack/Server/Server.csproj",
"projectName":"Server","projectPath":"/Users/mac/Documents/GitHub/test-task-for-backend-stack/Server/Server.csproj",
"outputPath":"/Users/mac/Documents/GitHub/test-task-for-backend-stack/Server/obj/","projectStyle":
"PackageReference","originalTargetFrameworks":["net6.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},
"frameworks":{"net6.0":{"targetAlias":"net6.0","projectReferences":{}}},
"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net6.0":
{"targetAlias":"net6.0","dependencies":{"EntityFramework":
{"target":"Package","version":"[6.4.4, )"},
"Microsoft.EntityFrameworkCore.Design":{"include":"Runtime, Build, Native, ContentFiles, Analyzers, BuildTransitive",
"suppressParent":"All","target":"Package","version":"[5.0.0, )"},
"Microsoft.EntityFrameworkCore.SqlServer":{"target":"Package","version":"[5.0.0, )"},
"Swashbuckle.AspNetCore":{"target":"Package","version":"[5.6.3, )"}},
"imports":["net461","net462","net47","net471","net472","net48"],
"assetTargetFallback":true,"warn":true,
"frameworkReferences":{"Microsoft.AspNetCore.App":{"privateAssets":"none"},
"Microsoft.NETCore.App":{"privateAssets":"all"}},
"runtimeIdentifierGraphPath":"/usr/local/share/dotnet/sdk/6.0.100-preview.6.21355.2/RuntimeIdentifierGraph.json"}}
已经过去几天了。我变得非常困惑和疯狂。 为什么会发生这种情况......以及为什么互联网上没有太多关于此类错误的信息。请给我指出正确的方向..
net6.0
目标框架,但在您安装了 EF6 时仍未发布,它是以前的迭代实体框架(主要用于旧版 .NET Framework 项目),并且您还拥有 EF Core(它)但旧版本 - 5.0(您实际上在您的上下文中使用它,请参阅那里的 using Microsoft.EntityFrameworkCore;
语句)。
尝试删除
EntityFramework
软件包并安装 Microsoft.EntityFrameworkCore.SqlServer
的预览版(可能只需更新到最新 5 版本也有帮助),然后完全删除或安装 Microsoft.EntityFrameworkCore.Design
的预览版。 (我还建议将您的 SDK 更新为 rc 并安装 rc 版本的软件包)。
或者尝试删除对
EntityFramework
(不是核心框架)的引用并将目标框架更改为 net5.0
(如果您的计算机上安装了它)。
至于为什么会看到这个异常 - 我猜它与 .NET 6 中添加到
Queryable
的新方法有关,这使得 this 检查之一失败。
TL;博士
如评论中所述 - 将 EF Core 更新到相应的最新版本(适用于 5.0 和 3.1)或更新到 .NET 6.0 和 EF Core 6。
我有 PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.10"。该问题在 5.0.13 上已解决。 谢谢你