简体   繁体   English

EF Core 5 - 多对多 - 删除

[英]EF Core 5 - Many to Many - remove

I am using new feature of EF Core 5 where I dont have to specify many-to-many class by myself and instead let it do for EF itself.我正在使用 EF Core 5 的新功能,我不必自己指定多对多 class 而是让它为 EF 本身做。

I have a simple classes:我有一个简单的课程:

UserMap:用户地图:

public class UserMap : IEntityTypeConfiguration<User>
    {
        public void Configure(EntityTypeBuilder<User> builder)
        {
            builder.HasKey(x => x.Id);    

            builder.HasMany(x => x.Favorites).WithMany(x => x.Followers);
        }
    }


public class RecipeProviderMap : IEntityTypeConfiguration<RecipeProvider>
    {
        public void Configure(EntityTypeBuilder<RecipeProvider> builder)
        {
            builder.HasKey(x => x.Id);    

            builder.HasMany(x => x.Followers).WithMany(x => x.Favorites);
        }
    }

And of course collections in each class like:当然,每个 class 中的 collections 就像:

public ICollection<User> Followers { get; set; }
public ICollection<RecipeProvider> Favorites { get; set; }

If I want to add an follower to RecipeProvider class I do:如果我想向 RecipeProvider class 添加关注者,我会这样做:

var recipeProvider = await _dataProvider
                .RecipeProviders
                .Include(x => x.Followers)
                .FirstOrDefaultAsync(x => x.Id == request.RecipeProviderId, cancellationToken);

            if (recipeProvider == null)
                return Unit.Value;

            recipeProvider.AddAsFavorite(user);

            _dataWriter.Update(recipeProvider);
            await _dataWriter.SaveChangesAsync();

Not that line HAS to be added不必添加该行

_dataWriter.Update(recipeProvider); _dataWriter.Update(recipeProvider);

So above example work for adding but I have problem with removing and I tried a lot of different options.所以上面的示例适用于添加,但我在删除时遇到了问题,我尝试了很多不同的选项。 The proposal looks like:该提案如下所示:

 var user = await 
                _dataProvider
                    .Users
                    .Include(x => x.Favorites)
                    .FirstOrDefaultAsync(x => x.Id == request.FollowerId, cancellationToken);    

            var provider = user.Favorites.FirstOrDefault(x => x.Id == request.RecipeProviderId);

            user.Favorites.Remove(provider);
            await _dataWriter.SaveChangesAsync();

And above code do not remove the entity.上面的代码不会删除实体。 Am I missing something?我错过了什么吗?

I took your code and made a similar model but with books and tags.我拿了你的代码,做了一个类似的 model 但有书籍和标签。

var tag = dbCotnext.Find<Tag>(1);
var book = dbCotnext.Find<Book>(1);

tag.Books.Add(book);
dbCotnext.SaveChanges();
tag.Books.Remove(book);
dbCotnext.SaveChanges();

I placed breakpoints after both SaveChanges and everything in the database looked as expected, I see the new entity in the BookTag table after hitting the first breakpoint and it vanishes after I hit the second.我在 SaveChanges 和数据库中的所有内容都按预期放置之后放置了断点,我在击中第一个断点后看到 BookTag 表中的新实体,在我击中第二个断点后它消失了。

However, I see something alerting in your code.但是,我在您的代码中看到了一些警告。 You're using two contexts, one for reads _dataProvider and the other for writes _dataWriter .您正在使用两种上下文,一种用于读取_dataProvider ,另一种用于写入_dataWriter I think that if you would call SaveChanges on the _dataProvider it would work, but you call it on _dataWriter where the entity you loaded from the other context is not tracked.我认为,如果您在_dataProvider上调用SaveChanges它将起作用,但是您在_dataWriter上调用它,您从其他上下文加载的实体不会被跟踪。 You could explicitly load it to _dataWriter and then remove the element from the collection and then save changes, so then SaveChanges would work as expected on the _dataWriter .您可以将其显式加载到_dataWriter ,然后从集合中删除该元素,然后保存更改,这样SaveChanges将在_dataWriter上按预期工作。

To sum up, I think that _dataWriter is not tracking changes on the entity loaded by another context which is _dataProvider .总而言之,我认为_dataWriter没有跟踪由另一个上下文加载的实体的更改,即_dataProvider

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM