-
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
Can not build relationships when attach disconnected entities which have many-to-many related entities #23787
Comments
I think I'm getting into a similar issue where using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using Xunit;
public class Tests
{
[Fact]
public void Test()
{
using (var ctx = new MyContext())
{
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
ctx.Attach(Cuisine.Chinese);
ctx.Add(new Restaurant { Cuisines = { Cuisine.Chinese } });
ctx.SaveChanges();
}
using (var ctx = new MyContext())
{
ctx.Attach(Cuisine.Chinese);
var restaurant = ctx.Set<Restaurant>().First();
Assert.NotEmpty(restaurant.Cuisines); // pass
restaurant.Cuisines.Clear();
Assert.Empty(restaurant.Cuisines); // pass
ctx.SaveChanges(); // save changes unexpectedly adds back the cuisines
Assert.Empty(restaurant.Cuisines); // fail
}
}
public class MyContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer("server=(localdb)\\mssqllocaldb;database=test");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Restaurant>().HasMany(_ => _.Cuisines).WithMany(_ => _.Restaurants);
modelBuilder.Entity<Cuisine>().HasData(Cuisine.Chinese);
}
}
public sealed class Restaurant
{
public int RestaurantId { get; set; }
public ICollection<Cuisine> Cuisines { get; } = new HashSet<Cuisine>();
}
public sealed class Cuisine
{
public static readonly Cuisine Chinese = new Cuisine { CuisineId = 1 };
public int CuisineId { get; set; }
public ICollection<Restaurant> Restaurants { get; } = new HashSet<Restaurant>();
}
} |
Confirmed this is a bug. Attach is not leaving the relationship state of entities set correctly. A workaround is to make a call to DetectChanges immediately after calling Attach. For example: var post = db.Posts.AsNoTracking().Include(x => x.Tags).FirstOrDefault();
db.Attach(post);
db.ChangeTracker.DetectChanges();
post.Tags.Clear();
db.SaveChanges(); |
@ajcvickers Thanks for workaround which passes the test on my previous example. Oh and Happy New Year to the EF Core team! 🥳 |
… with generated key values This is a precursor to fixes for #23659, #23787. This test has no product changes, it just refactors the many-to-many tests so that they can be run with generated key values, as well as running with explicit keys values like they currently do. Generated keys result in more work being done in fixup by navigations, which is where both of these issues live. Once this is merged I will send out separate PRs to fix the two bugs.
… with generated key values (#23807) This is a precursor to fixes for #23659, #23787. This test has no product changes, it just refactors the many-to-many tests so that they can be run with generated key values, as well as running with explicit keys values like they currently do. Generated keys result in more work being done in fixup by navigations, which is where both of these issues live. Once this is merged I will send out separate PRs to fix the two bugs.
File a bug
EF Core version: 5.0.1, 6.0.0-alpha.1.20622.2(daily build)
Database provider: Sqlite
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.8.1
Consider we have such entities and DbContext:
If we want to remove all tags from a post, we expect all these methods should work, but actually not:
Related tags of a disconnected post are tracked but not considered related to the post when attach the post to a DbContext.
The text was updated successfully, but these errors were encountered: