首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EFCore急切加载问题

EFCore急切加载问题
EN

Stack Overflow用户
提问于 2020-10-01 05:13:33
回答 1查看 63关注 0票数 0

我有点困惑,为什么这在EF Core 3.1.8和SQLite中不起作用。我在两个带有int和Guid的实体之间有一个复合外键关系。当我尝试立即加载具有存储区的所有存储区时,StoreSection导航属性不返回任何结果。

我已经检查了创建的表,数据正在按预期保存。我已经检查了生成的优化sql,当我直接运行它时,结果会返回我所期望的结果。下面是一个控制台应用程序,演示了这个问题。

代码语言:javascript
复制
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace TestEfcore
{
    class Program
    {
        static void Main()
        {
            Guid userId = Guid.NewGuid();

            using (var context = new GroceryManagerContext())
            {
                var newStore = new Store
                {
                    Name = "Grocery World",
                    GroceryManagerUserId = userId
                };

                newStore.StoreSections.Add(new StoreSection() { Name = "Deli", GroceryManagerUserId = userId });

                newStore.StoreSections.Add(new StoreSection() { Name = "Produce", GroceryManagerUserId = userId });

                context.Add(newStore);
                context.SaveChanges();
            }
            
            using (var context = new GroceryManagerContext())
            {
                // Attempting to eager load store sections.
                var stores = context.Stores
                   .Include(s => s.StoreSections)
                   .Where(s => s.GroceryManagerUserId == userId)
                   .ToList();

                Console.WriteLine($"The user guid is {userId}.");

                foreach (var store in stores)
                {
                    Console.WriteLine($"There are {store.StoreSections.Count} store sections in {store.Name}.");
                }
            }
        }

        public class Store
        {
            public int StoreId { get; set; }

            public string Name { get; set; } = string.Empty;

            public Guid GroceryManagerUserId { get; set; }

            public List<StoreSection> StoreSections { get; } = new List<StoreSection>();
        }

        public class StoreSection
        {
            public int StoreSectionId { get; set; }

            public string Name { get; set; } = string.Empty;

            public Guid GroceryManagerUserId { get; set; }

            public int StoreId { get; set; }
            public Store Store { get; set; } = new Store();
        }

        public class GroceryManagerContext : DbContext
        {
            public DbSet<Store>? Stores { get; set; }

            public DbSet<StoreSection>? StoreSections { get; set; }

            protected override void OnModelCreating(ModelBuilder builder)
            {
                if (builder == null)
                    throw new ArgumentNullException(nameof(builder));

                base.OnModelCreating(builder);

                builder.Entity<Store>()
                    .HasIndex(s => new { s.Name, s.GroceryManagerUserId })
                    .IsUnique();

                builder.Entity<StoreSection>()
                    .HasIndex(ss => new { ss.Name, ss.GroceryManagerUserId })
                    .IsUnique();

                builder.Entity<StoreSection>()
                    .HasOne(ss => ss.Store)
                    .WithMany(s => s.StoreSections)
                    .HasForeignKey(ss => new { ss.StoreId, ss.GroceryManagerUserId })
                    .HasPrincipalKey(s => new { s.StoreId, s.GroceryManagerUserId })
                    .IsRequired();
            }

            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.UseSqlite(@"Data Source=D:\src\TestEfcore\TestEfcore\GroceryManager.db");
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-01 09:22:21

不会立即加载StoreSections导航属性,因为您正在使用新运算符初始化StoreSection实体的Store属性。

这会导致StoreSection.StoreId属性与0一起保存,从而阻止创建关联

您需要更改StoreSection实体以删除初始化以解决问题:

代码语言:javascript
复制
public class StoreSection
{
    public int StoreSectionId { get; set; }

    public string Name { get; set; } = string.Empty;

    public Guid GroceryManagerUserId { get; set; }

    public int StoreId { get; set; }
    public Store Store { get; set; } // initialization has been removed here
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64145826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档