Entity Framework |序列包含多个匹配元素
Entity Framework | Sequence contains more than one matching element
我使用了数据库优先的方法。
模型是正确的(或者至少看起来像)
但我总是得到这个错误。拜托,我已经尝试了很多东西..
我的程序的完整代码(甚至 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
target framework which is still not released while you have installed EF6,这是以前的迭代 Entity Framework(主要用于遗留 .NET Framework 项目),您还有 EF Core(它的现代迭代)但旧版本 - 5.0 (您实际上在上下文中使用它,请参阅那里的 using Microsoft.EntityFrameworkCore;
语句)。
尝试删除 EntityFramework
软件包并安装 Microsoft.EntityFrameworkCore.SqlServer
(possibly just updating to the latest 5 version also can help) and either removing completely or installing preview version of Microsoft.EntityFrameworkCore.Design
的预览版。 (我还建议将您的 SDK 更新为 rc 并安装 rc 版本的包)。
或者尝试删除对 EntityFramework
(不是 Core one)的引用并将目标框架更改为 net5.0
(如果您的机器上安装了它)。
至于您为什么会看到此异常 - 我猜想这与添加到 Queryable
in .NET 6 which made one of this 检查失败的新方法有关。
TL;DR
如评论中所述 - 将 EF Core 更新到相应的最新版本(适用于 5.0 和 3.1)或更新到 .NET 6.0 和 EF Core 6。
我使用了数据库优先的方法。 模型是正确的(或者至少看起来像) 但我总是得到这个错误。拜托,我已经尝试了很多东西.. 我的程序的完整代码(甚至 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
target framework which is still not released while you have installed EF6,这是以前的迭代 Entity Framework(主要用于遗留 .NET Framework 项目),您还有 EF Core(它的现代迭代)但旧版本 - 5.0 (您实际上在上下文中使用它,请参阅那里的 using Microsoft.EntityFrameworkCore;
语句)。
尝试删除 EntityFramework
软件包并安装 Microsoft.EntityFrameworkCore.SqlServer
(possibly just updating to the latest 5 version also can help) and either removing completely or installing preview version of Microsoft.EntityFrameworkCore.Design
的预览版。 (我还建议将您的 SDK 更新为 rc 并安装 rc 版本的包)。
或者尝试删除对 EntityFramework
(不是 Core one)的引用并将目标框架更改为 net5.0
(如果您的机器上安装了它)。
至于您为什么会看到此异常 - 我猜想这与添加到 Queryable
in .NET 6 which made one of this 检查失败的新方法有关。
TL;DR
如评论中所述 - 将 EF Core 更新到相应的最新版本(适用于 5.0 和 3.1)或更新到 .NET 6.0 和 EF Core 6。