ffernandolima / ef-core-data-access

It's a modern and generic data access structure for .NET and Microsoft.EntityFrameworkCore. It supports UnitOfWork, Repository and QueryBuilder patterns. It also includes auto history utilities, multiple databases support with distributed transactions and databases/tables sharding for some database providers.
MIT License
165 stars 25 forks source link

Include behavior different when using UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking) #18

Closed KennethScott closed 1 year ago

KennethScott commented 1 year ago

I've just started trying to implement this library into a project and I'm having trouble getting it to load a one-to-one relationship.

I have a User entity, and I have a Queue entity. There is a single queue/one-to-one for each user, so I have a Queue property on the User entity using the foreign key.

When I attempt to Include the Queue property, unless I have NoTracking set, it isn't populated.

var repository = _unitOfWork.Repository<User>();

var query = repository.SingleResultQuery()
          .AndFilter(u => u.UserId == userId)
          .Include(src => src.Include(u => u.Queue));
          /////.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

var user = await repository.SingleOrDefaultAsync(query);

user.Queue will be null here...

var query = repository.SingleResultQuery()
          .AndFilter(u => u.UserId == userId)
          .Include(src => src.Include(u => u.Queue));
          .UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

var user = await repository.SingleOrDefaultAsync(query);

user.Queue is correctly loaded here....

What am I doing wrong?

Thanks-

KennethScott commented 1 year ago

Turns out this was a bug in the app, and not specific to any one unit of work/repository pattern. Sorry for the false alarm.