Sav*_*lla 41 c# sql-server orm entity-framework entity-framework-core
我使用数据库优先方法。该模型是正确的(或者至少看起来是正确的)但我总是收到此错误。拜托,我已经尝试了很多事情..我的程序的完整代码(甚至是我创建数据库的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",
"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"}}
Run Code Online (Sandbox Code Playgroud)
已经过去几天了。我变得非常困惑和疯狂。为什么会发生这种情况......以及为什么互联网上没有太多关于此类错误的信息。请给我指出正确的方向..
Gur*_*ron 74
当您安装了 EF6 时,您的目标框架仍未发布,EF6是net6.0
以前的迭代实体框架(主要与旧版 .NET Framework 项目一起使用),并且您还有 EF Core(它的现代迭代),但较旧版本 - 5.0(您实际在上下文中使用的内容,请参阅那里的陈述)。using Microsoft.EntityFrameworkCore;
尝试删除EntityFramework
软件包并安装预览版本Microsoft.EntityFrameworkCore.SqlServer
(可能只需更新到最新的 5 版本也有帮助),然后完全删除或安装预览版本Microsoft.EntityFrameworkCore.Design
。(我还建议将您的 SDK 更新为 rc 并安装 rc 版本的软件包)。
或者尝试删除对(不是核心框架)的引用EntityFramework
并将目标框架更改为net5.0
(如果您的计算机上安装了它)。
Queryable
至于为什么会看到这个异常 - 我猜它与.NET 6 中添加的新方法有关,该方法导致其中一项检查失败。
长话短说
正如评论中提到的 - 将 EF Core 更新到相应的最新版本(适用于 5.0 和 3.1)或更新到 .NET 6.0 和 EF Core 6。
归档时间: |
|
查看次数: |
37800 次 |
最近记录: |