efcore icon indicating copy to clipboard operation
efcore copied to clipboard

QueryTrackingBehavior.NoTracking has different behavior in 'Debug' and 'Release' mode

Open MM-RYO opened this issue 4 years ago • 7 comments

We try to manipulate data from DbContext and used Include/ThenInclude methods to get referenced data. The code works fine in local Debug mode. It doesn't work if I publish codes in Visual Studio (Release mode). the included data is missing.

Here is the codes.

var data = DbContext.xxxParameterValues
                                .Include(x => x.ParameterValue)
                                .Include(x => x.ParameterValue.ParameterValueSet)
                                .Include(x => x.ParameterValue.ParameterDefinition)
                                .ThenInclude(x => x.Parent)
                                .Include(x => x.ParameterValue.ParameterDefinition.ParameterRules)
                                .ThenInclude(x => x.ParameterCriteriaAttributes)
                                .Include(x => x.ParameterValue.ParameterDefinition.ParameterRules)
                                .ThenInclude(x => x.ParameterRuleCriteria)
                                .Include(x => x.ParameterValue.ParameterDefinition.ParameterRules)
                                .ThenInclude(x => x.ParameterRuleArguments)
                                .Include(x => x.ParameterValue.ParameterValueSet)
                                .Where(x => x.EntityId == Id)
                                .ToList();

Provider and version information

EF Core version: 6.0.2 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET 6.0 (6.0.2) Operating system: Windows 10 x64 IDE: Visual Studio 2022 17.1

MM-RYO avatar Mar 04 '22 09:03 MM-RYO

This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Mar 04 '22 13:03 ajcvickers

Hi @ajcvickers,

As you requested, I have attached a sample for this with a Console app. it can be reproduced in both Debug/Release mode. But in our project we only can reproduce this in Release mode.

Here is the codes.

using Microsoft.EntityFrameworkCore;
using NoTrackingApp;

var contextOptions = new DbContextOptionsBuilder<TestContext>()
    .UseInMemoryDatabase("TestContext")
    .Options;

using var context = new TestContext(contextOptions);

context.Database.EnsureCreated();
context.Database.EnsureDeleted();

InitialData(context);

// Change the QueryTrackingBehavior to NoTracking
// Print:
//  Test1 => Lvl1 Count:0 => Lvl2 Count:0
//  Test2 => Lvl1 Count:0 => Lvl2 Count:0

// Change the QueryTrackingBehavior to TrackAll / NoTrackingWithIdentityResolution
// Print:
//  Test1 => Lvl1 Count:1 => Lvl2 Count:1
//  Test2 => Lvl1 Count:0 => Lvl2 Count:0

context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTrackingWithIdentityResolution;

var entities = context.Entities.Include(x => x.Parent)
    .ThenInclude(x => x.Lvl1Values)
    .ThenInclude(x => x.Lvl2Values)
    .ToList();

foreach (var item in entities)
    Console.WriteLine(
        $"{item.Name} => Lvl1 Count:{item.Lvl1Values?.Count ?? 0} => Lvl2 Count:{item.Lvl1Values?.SelectMany(x => x.Lvl2Values).Count() ?? 0}");

Console.ReadKey();

void InitialData(TestContext context)
{
    var test1 = new Entity { Id = 1, Name = "Test1", ParentId = null };
    var test2 = new Entity { Id = 2, Name = "Test2", ParentId = 1 };
    context.Entities.AddRange(test1, test2);
    var lvl11Value = new Lvl1Value { EntityId = 1, Id = 10 };
    context.Lvl1Values.Add(lvl11Value);
    var lvl21Value = new Lvl2Value { Lvl1ValueId = 10, Id = 20 };
    context.Lvl2Values.Add(lvl21Value);
    context.SaveChanges();
}

NoTrackingApp.zip

MM-RYO avatar Mar 07 '22 05:03 MM-RYO

@robinNode What issue are you reporting here? The original issue stated, "has different behavior in 'Debug' and 'Release' mode," but now you say, "it can be reproduced in both Debug/Release mode." What is the issue that can be reproduced?

ajcvickers avatar Mar 07 '22 11:03 ajcvickers

@robinNode What issue are you reporting here? The original issue stated, "has different behavior in 'Debug' and 'Release' mode," but now you say, "it can be reproduced in both Debug/Release mode." What is the issue that can be reproduced?

Yes. Previously, I only can reproduce this issue in Release mode in my company project. But now, I can reproduce this issue in both Debug/Release mode in my Demo.

The issue is: the property 'Lvl1Values' and 'Lvl2Values' couldn't be loaded by calling 'ThenInclude' with QueryTrackingBehavior.NoTracking You should see this comment in the above code block.

// Change the QueryTrackingBehavior to NoTracking
// Print:
//  Test1 => Lvl1 Count:0 => Lvl2 Count:0
//  Test2 => Lvl1 Count:0 => Lvl2 Count:0

// Change the QueryTrackingBehavior to TrackAll / NoTrackingWithIdentityResolution
// Print:
//  Test1 => Lvl1 Count:1 => Lvl2 Count:1
//  Test2 => Lvl1 Count:0 => Lvl2 Count:0

MM-RYO avatar Mar 08 '22 01:03 MM-RYO

This is by design. In the example provided, you are first including parent and only then include lev1values and lvl2values. So we only populate lvl1values on the parent but not on the root entity. Identity resolution makes them appear on the root level also, but for pure NoTracking we don't do anything.

foreach (var item in entities)
{
    Console.WriteLine(
        $"{item.Name} => Lvl1 Count:{item.Lvl1Values?.Count ?? 0} => Lvl2 Count:{item.Lvl1Values?.SelectMany(x => x.Lvl2Values).Count() ?? 0}");

    var parent = item.Parent;
    Console.WriteLine(
        $"PARENT: {parent?.Name} => Lvl1 Count:{parent?.Lvl1Values?.Count ?? 0} => Lvl2 Count:{parent?.Lvl1Values?.SelectMany(x => x.Lvl2Values).Count() ?? 0}");
}

produces the following output for NoTracking:

Test1 => Lvl1 Count:0 => Lvl2 Count:0
PARENT:  => Lvl1 Count:0 => Lvl2 Count:0
Test2 => Lvl1 Count:0 => Lvl2 Count:0
PARENT: Test1 => Lvl1 Count:1 => Lvl2 Count:1

maumar avatar Mar 12 '22 04:03 maumar

Hi @maumar, I still want to know why 'NoTracking' has different behavior between Release and Debug.

MM-RYO avatar Mar 21 '22 03:03 MM-RYO

@robinNode The behavior should not be different. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

ajcvickers avatar Mar 21 '22 08:03 ajcvickers