Efcore: Changes to entities ignored while looping through change tracker entries

Created on 23 Sep 2019  路  8Comments  路  Source: dotnet/efcore

I have some code that I run just before SaveChanges is called. This code loops through the entries currently in the change tracker and changes a few values of the underlying entities.

During upgrade to 3.0.0-rc1.19456.14 I noticed that these changes are now ignored. I've also verified this in 3.0.0-rc2.19463.9 where this still happens. Edit: Also still happens on 3.0.0. (Congratulations on the release, btw! 馃槃)

This happens with the SqlServer provider, and it doesn't happen with the InMemory provider.

Steps to reproduce

The following code ran in 2.2.x, because the foreign key was set to null while looping the entries. Now, because this change is ignored, it throws because an entity with the foreign key 999 doesn't exist.
``` c#
public class Tests
{
[Fact]
public ShouldNotThrow()
{
var dbContext = new MyContext();
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
dbContext.Set().Add(new Entity() { SubEntityId = 999 });

    foreach (var entry in dbContext.ChangeTracker.Entries())
    {
        if (entry.Entity is Entity entity)
        {
            entity.SubEntityId = null;
        }
    }

    dbContext.SaveChanges();
}

}

public class MyContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=localhost;Database=Test;Integrated Security=False;User ID=sa;Password={my-pass}");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Entity>();
    modelBuilder.Entity<SubEntity>();
}

}

public class Entity
{
public int Id { get; set; }

public int? SubEntityId { get; set; }
public SubEntity SubEntity { get; set; }

}

public class SubEntity
{
public int Id { get; set; }
}
```

Further technical details

EF Core version: 3.0.0-rc1.19456.14 / 3.0.0-rc2.19463.9 / 3.0.0
Database provider: SqlServer
Target framework: netcoreapp3.0
Operating system: Win 10 1903
IDE: Visual Studio 2019 16.3.0

closed-fixed customer-reported type-bug

Most helpful comment

Verified that this reproduce in 3.0

All 8 comments

Wait for @ajcvickers

Verified that this reproduce in 3.0

I stumbled across this too while testing out EF 3.0 RC and release, it was causing weird things to happen in some spots. I was too focused on other things to report it, but its definitely going to make our codebase stay on 2.6 for a while longer.

I hope this can gets fixed soon.

It seems this is planned to be fixed in 3.1.0. Is there perhaps any known workaround we can use in the meantime? Else we will have to wait till November until we can upgrade to ASP.NET Core 3.x.

@nphmuller Based on the root cause, workarounds might be:

  • Set actual values before calling Add
  • When setting a value to null/CLR default, also force the temporary value to the same thing.

@ajcvickers Finally Something Crunchy? 馃槃
Thanks for fixing this. Do you recommend trying it out in the nightly if we want to test it or do you think it would be better to wait until 3.1 has an RC?

Since 3.1 will mainly contain bug fixes I think it鈥檚 worth trying out the nightlies.
Thanks @ajcvickers !

@dragnilar @nphmuller Always the nightlies, otherwise we won't have enough time to respond if you find issues.

Was this page helpful?
0 / 5 - 0 ratings