Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should we be treating objects created by the user in a query differently from the ones we materialize? #12668

Closed
divega opened this issue Jul 13, 2018 · 3 comments

Comments

@divega
Copy link
Contributor

divega commented Jul 13, 2018

Currently we have an implicit assumption that if a user writes to code to explicitly create instances of a mapped type, it is ok not to track it.

Creating this issue to have an opportunity to (re)discuss if this what we want, and how it plays with different features, present and future, like shadow properties and customer materialization through factory methods, etc.

@Condra963
Copy link

As @divega says it is ok not to track this kind of action for the user because, in my case i only use this in readonly scenarios. And as we see in the comment of the AsNoTracking method:
Disabling change tracking is useful for read-only scenarios because it avoids the overhead of setting up change tracking for each entity instance.

So in my case you should not treat the object created by the user in a query differently from the ones you materialize. And it's quite strange you treat it differently because i query Entity X and i want to create Entity X.
Maybe you should allow it that an Entity stays an Entity and does not become an Object when i apply AsNoTracking to my query. Then you give the user the choice what they want?

Without AsNoTracking

var test = _testsDbContext.Entities
    .GroupJoin(
        _testsDbContext.Joins,
        e => e.Name,
        t => t.Key,
        (e, t) => new { Entity = e, Joins = t })
    .SelectMany(
        group => group.Joins.DefaultIfEmpty(),
        (group, t) => new { group.Entity, JoinName = t.Value })
    .Select(group => new Entity() // Becomes an object
    {
        Id = group.Entity.Id,
        Name = group.JoinName ?? group.Entity.Name,
        EntityParentId = group.Entity.EntityParentId,
        EntityParent = group.Entity.EntityParent // Becomes a loaded property
    })
    .ToList();

With AsNoTracking

var test = _testsDbContext.Entities
    .AsNoTracking()
    .GroupJoin(
        _testsDbContext.Joins,
        e => e.Name,
        t => t.Key,
        (e, t) => new { Entity = e, Joins = t })
    .SelectMany(
        group => group.Joins.DefaultIfEmpty(),
        (group, t) => new { group.Entity, JoinName = t.Value })
    .Select(group => new Entity() // Stays an entity
    {
        Id = group.Entity.Id,
        Name = group.JoinName ?? group.Entity.Name,
        EntityParentId = group.Entity.EntityParentId,
        EntityParent = group.Entity.EntityParent // Stays a navigation property
    })
    .ToList();

@ajcvickers
Copy link
Member

@smitpatel to cleanup.

@smitpatel
Copy link
Member

We decided in meeting that, we will track entities in result if and only if they are created through materialization pipeline. Anything else we don't track.

@smitpatel smitpatel removed this from the 3.0.0 milestone Jan 25, 2019
@smitpatel smitpatel removed their assignment Jan 12, 2022
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants