Configuration.LazyLoadingEnabled = false has no effect
Ashley-Clementi opened this issue · 3 comments
Description
I have created a simple program where loading of related entities are expected to NOT occur, but they are loaded.
This is using DbConnection connection = Effort.DbConnectionFactory.CreateTransient();
With the observed behaviour Effort our confidence in testing is dramatically reduced as it does not find missing Include
Project
Further technical details
- EF version: 6.0.0
- EF Effort version: 2.2.2
- NMemory version: 3.1.0
- Database Provider:
Hello @Ashley-Clementi ,
Thank you for reporting, we will look at your issue.
Best Regards,
Jonathan
Performance Libraries
context.BulkInsert(list, options => options.BatchSize = 1000);
Entity Framework Extensions • Entity Framework Classic • Bulk Operations • Dapper Plus
Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval Function • SQL Eval Function
Hello @Ashley-Clementi ,
This is exactly how the tracking work.
You create a context, add a customer WITH
an EmailAddresses, save then retry the customer.
When Entity Framework retrieves the customer, it takes customer from the Change Tracke rif they exist, not the one returned by the "database". The one that already exists in the Change Tracker is the one you saved (so contains the EmailAddresses).
If you take the information from a new Context, you will retrieve the customer without the EmailAddresses
Let me know if that's clear.
Hello @JonathanMagnan
Thankyou for the explanation which is verified by changing the Main() as follows:
public static void Main()
{
DbConnection connection = Effort.DbConnectionFactory.CreateTransient();
using (var context = new EntityContext(connection))
{
context.Customers.AddRange(GetCustomers(3));
context.SaveChanges();
var allEmailddresses = context.EmailAddresses.ToList();
// The below variables HAVE EmailAddresses because it is using the same context
var all = context.Set<Customer>().ToList();
var allCustomers = context.Customers.ToList();
var firstCustomer = context.Customers.Where(x => x.Name == "Customer_1").ToList();
}
using (var context = new EntityContext(connection))
{
// Expect the below variables to NOT have any EmailAddresses because of the
// Configuration.LazyLoadingEnabled = false;
var all = context.Set<Customer>().ToList();
var allCustomers = context.Customers.ToList();
var firstCustomer = context.Customers.Where(x => x.Name == "Customer_1").ToList();
}
using (var context = new EntityContext(connection))
{
// Expect the below variables HAVE EmailAddresses because of the Include
var allWithEmailAddresses = context.Set<Customer>().Include("EmailAddresses").ToList();
}
}