繁体   English   中英

实体框架 | 序列包含多个匹配元素

[英]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目标框架,但在安装EF6时仍未发布,EF6 是以前的迭代实体框架(主要用于旧版 .NET Framework 项目),并且您还有 EF Core(它的现代迭代)但旧版本- 5.0(您实际用于您的上下文,请参阅using Microsoft.EntityFrameworkCore;那里的语句)。

尝试删除EntityFramework包并安装Microsoft.EntityFrameworkCore.SqlServer的预览版(可能只是更新到最新的 5 版本也有帮助),然后完全删除或安装Microsoft.EntityFrameworkCore.Design的预览版。 (我还建议将您的 SDK 更新为 rc 并安装 rc 版本的软件包)。

或者尝试删除对EntityFramework (不是 Core one)的引用并将目标框架更改为net5.0 (如果您的机器上安装了它)。

至于为什么你会看到这个异常——我猜它与 .NET 6 中添加到Queryable的新方法有关, 使得其中一项检查失败。

TL;博士

如评论中所述 - 将 EF Core 更新到相应的最新版本(适用于 5.0 和 3.1)或更新到 .NET 6.0 和 EF Core 6。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM