-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
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 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:
|
Add these 2 lines at the end of your test
Console.WriteLine($"Is Detached: {context.Entry(entity).State == EntityState.Detached}");
Console.WriteLine($"Now Detached: {context.Blogs.Local.Contains(entity)}");
When you change state from Unchanged to Detached state manager does not move entry to detached reference map.
But when you request entry for untracked entity it registers entry in detached reference map.
From: Arthur Vickers ***@***.***>
Sent: Monday, January 3, 2022 4:30 PM
To: dotnet/efcore ***@***.***>
Cc: GromanSoft ***@***.***>; Mention ***@***.***>
Subject: Re: [dotnet/efcore] LocalView<TEntity>.Contains returns true for detached entity (Issue #27056)
@GromanSoft<https://github.com/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
—
Reply to this email directly, view it on GitHub<#27056 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AXAMCKGTRTCTEPZ42TVL6QDUUGQFZANCNFSM5KR6AR5A>.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
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
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
class: Microsoft.EntityFrameworkCore.ChangeTracking.LocalView
method: Contains
bug: returns true for detached entity
solution: also check entry.EntityState != EntityState.Detached
The text was updated successfully, but these errors were encountered: