title: Disable Lazy Loading in Entity Framework Core
body: <p>There are plenty of posts about how to disable lazy loading in Entity Framework, but the same techniques don't work in EF Core. I found the <code>LazyLoadingEnabled</code> property in the change tracker, but this doesn't seem to work at all.</p>
  
  <p>Everything points to this in EF:</p>
  
  <pre><code>this.Configuration.LazyLoadingEnabled = false;
  </code></pre>
  
  <p>But, the <code>Configuration</code> property is missing in EF Core.</p>
  
  <p>Here is an example of what I am talking about:</p>
  
  <pre><code>public class TestContext : DbContext
  {
      public DbSet&lt;Person&gt; People { get; set; }
      public DbSet&lt;Address&gt; 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");
              }
          }
      }
  }
  </code></pre>
  
  <p>The above throws an exception because <code>BillingAddress</code> is not getting loaded even though I turned lazy loading off.</p>
  
  <p>I suspect this is a bug, but please tell me it isn't. I logged it here: <a href="https://github.com/aspnet/EntityFrameworkCore/issues/15802" rel="noreferrer">https://github.com/aspnet/EntityFrameworkCore/issues/15802</a></p>
  
  <p>You can download the sample here:
  <a href="https://www.dropbox.com/s/mimvgvcmibr7em2/EFSQLiteTest.7z?dl=0" rel="noreferrer">https://www.dropbox.com/s/mimvgvcmibr7em2/EFSQLiteTest.7z?dl=0</a></p>