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

Detaching an entity results in modify references #19203

Closed
AR1ES opened this issue Dec 6, 2019 · 1 comment · Fixed by #19432
Closed

Detaching an entity results in modify references #19203

AR1ES opened this issue Dec 6, 2019 · 1 comment · Fixed by #19432
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@AR1ES
Copy link

AR1ES commented Dec 6, 2019

I disable AutoDetectChanges and load some related records from different tables. When I detach some entity, all references to it are automatically nullified.
Everything worked well on EF Core 2.*.

Steps to reproduce

public class Class1 {
	public long Id { get; set; }
	public long? Class2Id { get; set; }
	public Class2 Class2 { get; set; }
}
	
public class Class2 {
	public long Id { get; set; }
}
...
public void TestMethod() {
	using(var db = new MyContext()) {
		db.ChangeTracker.AutoDetectChangesEnabled = false;
		var t1 = db.Class1.Include(m => m.Class2).FirstOrDefault();
		
		...
		// db.Entry(t1).State - Unchanged
		
		// detaching sub entity
		db.Entry(t1.Class2).State = EntityState.Detached;
		
		// result: 
		// db.Entry(t1).State - Modified
		// db.Entry(t1).Reference(m => m.Class2) - IsModified = true
		// db.Entry(t1).Property(m => m.Class2Id) - IsModified = true
	}
}

Further technical details

EF Core version: 3.1.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.4

@AR1ES AR1ES added the type-bug label Dec 6, 2019
@ajcvickers
Copy link
Contributor

Note for triage: It looks like detaching is marking an FK as modified when it should not. Full repro:

public class Class1 
{
    public long Id { get; set; }
    public long? Class2Id { get; set; }
    public Class2 Class2 { get; set; }
}
	
public class Class2 
{
    public long Id { get; set; }
}

public class BloggingContext : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
    }
    
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Class1>();
        modelBuilder.Entity<Class2>();
    }
}

public class Program
{
    public static async Task Main()
    { 
        using (var context = new BloggingContext())
        {
            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            context.Add(new Class1
            {
                Class2 = new Class2()
            });
            
            await context.SaveChangesAsync();
        }

        using (var context = new BloggingContext())
        {
                context.ChangeTracker.AutoDetectChangesEnabled = false;
                var t1 = context.Set<Class1>().Include(m => m.Class2).FirstOrDefault();
                var t2 = t1.Class2;
		
                Console.WriteLine("T1>" + context.Entry(t1).State);
                Console.WriteLine("T2>" + context.Entry(t2).State);

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

                Console.WriteLine("T1>" + context.Entry(t1).State);
                Console.WriteLine("T2>" + context.Entry(t2).State);
        }
    }
}

@ajcvickers ajcvickers added this to the 5.0.0 milestone Dec 9, 2019
@ajcvickers ajcvickers self-assigned this Dec 9, 2019
@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 Dec 30, 2019
@ajcvickers ajcvickers modified the milestones: 5.0.0, 3.1.x Feb 8, 2020
@ajcvickers ajcvickers reopened this Feb 8, 2020
ajcvickers added a commit that referenced this issue Feb 13, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
ajcvickers added a commit that referenced this issue Feb 13, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
ajcvickers added a commit that referenced this issue Feb 15, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
@ajcvickers ajcvickers modified the milestones: 3.1.x, 3.1.3 Feb 15, 2020
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants