鑒于 Post 的對象模型有很多 PostTranslation,我設(shè)置了兩個對象:public partial class Post{ public Post() { PostTranslations = new HashSet<PostTranslation>(); } public int Id { get; set; } public string Name { get; set; } public ICollection<PostTranslation> PostTranslations { get; set; }}public partial class PostTranslation { public int Id { get; set; } public string Title { get; set; } public int PostId { get; set; } public Post Post { get; set; }}在我的上下文類中,在 OnModelBuilding 中,我有以下內(nèi)容:modelBuilder.Entity<Post>(entity => { entity.ToTable("Posts"); entity .HasMany<PostTranslation>(x => x.PostTranslations) .WithOne(g => g.Post) .HasForeignKey(g=>g.PostId);});在我的存儲庫中,我調(diào)用以下內(nèi)容:db.Posts.Include(t=>t.PostTranslations).ToList();但在檢查生成的 SQL 時,PostTranslations 從未加入,并且 Post 對象上的哈希集始終為零。有翻譯,它們在數(shù)據(jù)庫中正確連接。如果我直接查詢它們,我可以從數(shù)據(jù)上下文中獲取 PostTranslations。但我似乎無法讓 EFCore 使用簡單的 .Include 語句急切加載它們。我錯過了什么?編輯 生成的 SQL 是這樣的:SELECT [f].[Id], [f].[Name] FROM [common].[Posts] AS [f]我預(yù)計會在那里看到 PostTranslations 的左連接,所以在我迭代 ToList() 中的變量之后,翻譯就會在那里。并且在模型構(gòu)建器中設(shè)置了“通用”模式:modelBuilder.HasDefaultSchema("common");
1 回答

白板的微信
TA貢獻(xiàn)1883條經(jīng)驗 獲得超3個贊
事實證明,這是一個命名空間問題。這是一個重構(gòu)的存儲庫,在它的頂部,有:
using System.Data.Entity;
使用該命名空間,正確編譯了 .Include 語句,但 EFCore 沒有獲取關(guān)系。添加:
using Microsoft.EntityFrameworkCore;
突然,SQL 正在為急切的負(fù)載正確生成連接。
- 1 回答
- 0 關(guān)注
- 131 瀏覽
添加回答
舉報
0/150
提交
取消