【问题标题】:Disable Lazy Loading in Entity Framework Core [duplicate]在实体框架核心中禁用延迟加载[重复]
【发布时间】:2019-10-11 14:07:33
【问题描述】:

有很多关于如何在 Entity Framework 中禁用延迟加载的帖子,但相同的技术在 EF Core 中不起作用。我在更改跟踪器中找到了LazyLoadingEnabled 属性,但这似乎根本不起作用。

EF 中的一切都指向这一点:

this.Configuration.LazyLoadingEnabled = false;

但是,EF Core 中缺少 Configuration 属性。

这是我所说的一个例子:

public class TestContext : DbContext
{
    public DbSet<Person> People { get; set; }
    public DbSet<Address> Addresses { get; set; }

    public TestContext()
    {
        this.ChangeTracker.LazyLoadingEnabled = false;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {            
        var connection = new SqliteConnection($"Data Source=Test.db");
        connection.Open();

        var command = connection.CreateCommand();

        command.CommandText = $"PRAGMA foreign_keys = ON;";
        command.ExecuteNonQuery();

        optionsBuilder.UseSqlite(connection);
        optionsBuilder.UseLazyLoadingProxies(false);

        base.OnConfiguring(optionsBuilder);
    }

    private static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        using (var context = new TestContext())
        {
            context.Database.EnsureCreated();

            var personKey = Guid.NewGuid().ToString();
            var addressKey = Guid.NewGuid().ToString();

            context.People.Add(new Entities.Person { PersonKey = personKey, BillingAddress = new Entities.Address { AddressKey = addressKey } });
            context.SaveChanges();
        }

        using (var context = new TestContext())
        {
            var people = context.People.ToList();

            foreach (var person in people)
            {
                if (person.BillingAddress == null) throw new Exception("The billing address wasn't loaded");
            }
        }
    }
}

上面会抛出异常,因为即使我关闭了延迟加载,BillingAddress 也没有加载。

我怀疑这是一个错误,但请告诉我不是。我在这里记录:https://github.com/aspnet/EntityFrameworkCore/issues/15802

您可以在此处下载示例: https://www.dropbox.com/s/mimvgvcmibr7em2/EFSQLiteTest.7z?dl=0

【问题讨论】:

  • 对于延迟加载 off 的含义似乎有些混淆。当延迟加载关闭时,EF Core 不会自动加载相关数据 - 您必须使用急切或显式加载显式执行此操作。所以这个异常是意料之中的,并证明 EF Core 做了它应该做的事情。为了让 EF Core 在首次访问导航属性时自动加载相关数据,您需要执行相反的操作 - 打开延迟加载 on
  • 我都试过了,但都没有加载 BillingAddress。没有极端的技巧就无法加载所有子实体。
  • 我不想延迟加载。我想一开始就加载所有实体。
  • 禁用延迟加载不会这样做。这并不意味着“自动加载所有内容”。这样的选项仍然不存在,所以我的回答 stackoverflow.com/questions/49593482/… 仍然适用。
  • 急切地加载所有相关数据很少是您想要的;大多数时候,您只需将整个数据库拉入本地内存。请记住,每个导航属性访问都是另一个(LEFT OUTER)连接,由于返回的行和列数量庞大,这将使查询变得非常缓慢以及实现。坦率地说,我认为这样的功能不应该存在,如果 EF 团队计划它,它应该需要奇怪的 hack。您似乎对什么是延迟加载和什么是急切加载感到困惑,您可能应该阅读一下。

标签: c# sqlite .net-core entity-framework-core


【解决方案1】:

如果您的问题是如何使用 EF Core 禁用 LazyLoading,请尝试:

this.ChangeTracker.LazyLoadingEnabled = false;

【讨论】:

  • 帮助我在按层次结构使用表时暂时禁用延迟加载;我收到“尝试延迟加载导航属性...”
  • 由于延迟加载在启动时/配置时启用,我被引导相信这个标志不再存在。非常感谢您纠正我的错误!
猜你喜欢
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多