小编Sav*_*lla的帖子

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

我使用数据库优先方法。该模型是正确的(或者至少看起来是正确的)但我总是收到此错误。拜托,我已经尝试了很多事情..我的程序的完整代码(甚至是我创建数据库的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();
    }
Run Code Online (Sandbox Code Playgroud)

这不是我第一次使用 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....
Run Code Online (Sandbox Code Playgroud)

这是我的包参考文件

"restore":{"projectUniqueName":"/Users/mac/Documents/GitHub/test-task-for-backend-stack/Server/Server.csproj", …
Run Code Online (Sandbox Code Playgroud)

c# sql-server orm entity-framework entity-framework-core

41
推荐指数
1
解决办法
4万
查看次数