4

I have 2 classes, SalesSubCategory and SalesCategory:

[Table("SALES.SubCategory")]
public class SalesSubCategory
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public int CategoryID { get; set; }

    public string Name { get; set; }

    [ForeignKey("CategoryID")]
    public SalesCategory SalesCategory { get; set; }
}

[Table("SALES.Category")]
public class SalesCategory
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    public string Name { get; set; }
}

This Method returns SalesSubCategories List WITHOUT the SalesCategory Object loaded

public class TestController : Controller
{
    private readonly MD_Context _context;

    public TestController(MD_Context context)
    {
        _context = context;
    }
    public async Task<List<SalesSubCategory>> NoRelated()
    {
        var subCategories = await _context.SalesSubCategories.ToListAsync();
        return subCategories;
    } 

Related Entity not Loaded

This Method returns SalesSubCategories List WITH the SalesCategory Object loaded

public async Task<List<SalesSubCategory>> Related() 
{
    var subCategories = await _context.SalesSubCategories.ToListAsync();
    var categories = await _context.SalesCategories.ToListAsync();
    return subCategories;
} 

Related Entity loaded

MD_Context is configured to have lazy loading disabled:

Configuration.LazyLoadingEnabled = false;

Is this expected behavior? My preferred result is to NOT have the SalesCategory object entities pre-loaded.

Thank you.

Gmd3
  • 55
  • 5

2 Answers2

1

Yes this is expected behavior.

When loading entities from a context, the entities in play get attached in order to keep track on any modification on them, that is way, you get the results due to previous loading.

You can solve it by requesting the query to execute in a non tracking fashion. Which will not make entityframework associate it with your new query.

var subCategories = await _context.SalesSubCategories.ToListAsync();
var categories = await _context.SalesCategories.AsNoTracking().ToListAsync();
Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
  • So looking around SO, it looks like there's no global setting to have this apply to all my queries... painful – Gmd3 Sep 23 '16 at 23:45
  • Every ORM is different, I guess EntityFramework decision for this behavior is because they believe people will use them not JUST for extracting results into a strongly type objects, but for also managing their query results objects. You can use Dapper.NET or some kind of a more stripped feature like ORM for your desired behavior, it will give you the ability for strongly type objects without the concept of db context. – Orel Eraki Sep 24 '16 at 00:04
0

well I see your point , and this could be a bug in entity framework, but I tested it locally, and enabling or disabling LazyLoadingEnabled doesn't help. I found when the nested object which is SalesCategory has another nested object then LazyLoadingEnabled will help and doesn't load the nested objects any more, which is strange, but this is how entity framework works

Tarek Abo ELkheir
  • 1,311
  • 10
  • 13