[英]Simple usage of EF Core 3 results in error: Sequence contains more than one matching element
我正在為我的應用程序測試 EF Core 3 的使用情況,並遵循 Microsoft Docs ( https://docs.microsoft.com/en-us/ef/core/get-started/? tabs=netcore-cli )
我正在使用相應的 PostgreSQL 連接器來連接到我的數據庫。
我創建了以下模型:
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int Rating { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public Blog Blog { get; set; }
}
然后,在程序啟動時,我嘗試在數據庫中插入一個新的“博客”。
using (var db = new BloggingContext())
{
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();
}
根據我的理解,這應該沒問題。 然后我創建遷移,並針對數據庫運行它們 - 並且可以看到創建的表,具有正確的主鍵和外鍵。
然后,我在啟動程序時收到此錯誤: System.TypeInitializationException: The type initializer for 'Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal.NpgsqlStringMethodTranslator' threw an exception. ---> System.InvalidOperationException: Sequence contains more than one matching element
System.TypeInitializationException: The type initializer for 'Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal.NpgsqlStringMethodTranslator' threw an exception. ---> System.InvalidOperationException: Sequence contains more than one matching element
我已經嘗試了很長時間來查看我做錯了什么,但似乎無法弄清楚。
這里有什么完全錯誤的嗎?
System.TypeInitializationException:“Npgsql.EntityFrameworkCore.PostgreSQL.Query.ExpressionTranslators.Internal.NpgsqlStringMethodTranslator”的類型初始值設定項引發異常。 ---> System.InvalidOperationException: 序列包含多個匹配元素
從異常消息中,類型NpgsqlStringMethodTranslator
調用NpgsqlStringMethodTranslator
.Single()
或.SingleOrDefault()
,同時初始化靜態成員。 但該序列有 2 個或更多項目。
這種類型的源代碼可在線獲得。 我看到調用.Single()
來定位 2 個系統方法和 2 個擴展方法。 然而,他們在我看來很好。 在每種情況下都應該有一個匹配結果。
您提到了 EF Core 3,我只能建議您驗證框架和 nuget 包的版本是否一致。 如果您看不到任何明顯的問題,也許可以將 .csproj 文件的相關部分添加到您的問題中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.