MikaelEliasson / EntityFramework.Utilities

Provides extensions for EntityFramework that doesn't exist out of the box like delete and update by query and bulk inserts
443 stars 175 forks source link

UpdateAll support for Many-to-Many relationships with join table #74

Open joakimskoog opened 8 years ago

joakimskoog commented 8 years ago

First of all; great library, it works perfectly for me in almost all scenarios. The only problem I have is that I can't use UpdateAll for updating navigation properties in many-to-many relationships as seen below:

public class Character
{
    ...
    public ICollection<Book> Books { get; set; } = new List<Book>();
}

public class Book
{
    ...
    public ICollection<CharacterEntity> Characters { get; set; } = new List<Character>();
}

Character configuration:

HasMany(c => c.Books)
    .WithMany(b => b.Characters)
    .Map(cb =>
    {
        cb.MapLeftKey("BookId");
        cb.MapRightKey("CharacterId");
        cb.ToTable("BookCharacters");
    });

The following code won't update the join table with the correct ID's. I guess that I can create a stored procedure for this instead but it would be nice to have support for this in EntityFramework.Utilities or maybe I'm thinking about this in the wrong way?

characterToUpdate.Books.Add(book);

EFBatchOperation.For(context, context.Characters).UpdateAll(charactersToUpdate, x => x.ColumnsToUpdate(y => y.Books));

Bouke commented 5 years ago

Did you ever find a good solution for this? I'm trying to do bulk inserts into a many-to-many, but haven't found a great way to do this using EF / EF.Utilities.

RudeySH commented 5 years ago

I might be off here because I rarely use many-to-many relationships, but what if you explicitly create a class that will store the many-to-many relationship for you? To my understanding, EF code-first will generate a table automatically to store the many-to-many relationship. I can see how it's tough for EFUtilities to make use of this generated table, as it does not actually exist in the code-first model. If you write a class that holds the many-to-many relationships, you're effectively replacing the many-to-many relationships with two one-to-many relationships. Might be worth looking into.

Bouke commented 5 years ago

That’s indeed possible, however a pain to work with. For example a parent-child relationship: parent.Children would become something like parent.children.Select(x => x.Child). The m2m relationship is part of the metadata, however I’m not sure how to retrieve and link it to the class’ property.

Op 10 jun. 2019 om 23:49 heeft Rudey notifications@github.com het volgende geschreven:

I might be off here because I rarely use many-to-many relationships, but what if you explicitly create a class that will store the many-to-many relationship for you? To my understanding, EF code-first will generate a table to store the many-to-many relationship, I can see how it's tough for EFUtilities to make use of this generated table. If you write a class that holds the many-to-many relationships, you're effectively replacing the many-to-many relationships with two one-to-many relationships. Might be worth looking into.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.