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 LazyLoadingEnabled
property in the change tracker, but this doesn't seem to work at all.
Everything points to this in EF:
this.Configuration.LazyLoadingEnabled = false;
But, the Configuration
property is missing in EF Core.
Here is an example of what I am talking about:
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");
}
}
}
}
The above throws an exception because BillingAddress
is not getting loaded even though I turned lazy loading off.
I suspect this is a bug, but please tell me it isn't. I logged it here: https://github.com/aspnet/EntityFrameworkCore/issues/15802
You can download the sample here: https://www.dropbox.com/s/mimvgvcmibr7em2/EFSQLiteTest.7z?dl=0
We can disable lazy loading for a particular entity or a context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false.
You can turn off deferred loading by setting DeferredLoadingEnabled to false .
EF Core will then enable lazy loading for any navigation property that can be overridden--that is, it must be virtual and on a class that can be inherited from. For example, in the following entities, the Post. Blog and Blog. Posts navigation properties will be lazy-loaded.
Lazy loading can be turned off for all entities in the context by setting a flag on the Configuration property to false as shown in the following example. After turning off lazy loading, now when you run the above example again you will see that the Enrollments are not loaded and only student data is retrieved.
If your problem is how to disable LazyLoading with EF Core try:
this.ChangeTracker.LazyLoadingEnabled = false;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With