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

LocalView<TEntity>.Contains returns true for detached entity #27056

Closed
GromanSoft opened this issue Dec 22, 2021 · 2 comments · Fixed by #28960
Closed

LocalView<TEntity>.Contains returns true for detached entity #27056

GromanSoft opened this issue Dec 22, 2021 · 2 comments · Fixed by #28960
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@GromanSoft
Copy link

class: Microsoft.EntityFrameworkCore.ChangeTracking.LocalView
method: Contains
bug: returns true for detached entity
solution: also check entry.EntityState != EntityState.Detached

@ajcvickers
Copy link
Member

@GromanSoft I'm not able to reproduce this--see my code below. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class SomeDbContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseInMemoryDatabase("Test")
            .EnableSensitiveDataLogging();

    public DbSet<Blog> Blogs { get; set; }
}

public class Program
{
    public static void Main()
    {
        using (var context = new SomeDbContext())
        {
            var entity = new Blog { Name = "OneUnicorn" };

            Console.WriteLine($"Not tracked: {context.Blogs.Local.Contains(entity)}");

            context.Add(entity);

            Console.WriteLine($"Added: {context.Blogs.Local.Contains(entity)}");

            context.SaveChanges();

            Console.WriteLine($"Saved: {context.Blogs.Local.Contains(entity)}");

            context.Entry(entity).State = EntityState.Detached;

            Console.WriteLine($"Detached: {context.Blogs.Local.Contains(entity)}");
        }
    }
}

Output:

Not tracked: False
Added: True
Saved: True
Detached: False

@GromanSoft
Copy link
Author

GromanSoft commented Jan 3, 2022 via email

@ajcvickers ajcvickers self-assigned this Jan 3, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Jan 5, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 2, 2022
ajcvickers added a commit that referenced this issue Sep 2, 2022
Fixes #27056 (`LocalView<TEntity>.Contains` returns true for detached entity)
Fixes #27750 (`DbSet<T>.Local.Count` not updated when calling `ChangeTracker.Clear()`)
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc2 Sep 9, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-change-tracking closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants