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

Design question: API for bulk inserting child entities #42

Open MikaelEliasson opened 9 years ago

MikaelEliasson commented 9 years ago

Right now EFUtilities can only insert into a single table in each call. That means that an entity with child entities would have to be inserted with two call.

EFBatchOperation.For(db, db.BlogPosts).InsertAll(posts);
EFBatchOperation.For(db, db.BlogPostTags).InsertAll(posts.SelectMany(p => p.Tags));

This could be made easier. My question is how far I should take it. My personal feeling is that it's better to make an API that's simple and gives you control. Rember that these methods are there to insert really large number of items efficiently.

My thought is that it would require explicitly setting which child collections to insert.

EFBatchOperation.For(db, db.BlogPosts).InsertAll(posts,  setting => setting.Include(p => p.Tags));

Or with nesting:

EFBatchOperation.For(db, db.BlogPosts).InsertAll(posts, setting => setting.Include(p => p.Comments, setting2 => setting2.Include(c => c.Votes)));

Or with EF7 style includes:

EFBatchOperation.For(db, db.BlogPosts).InsertAll(posts, setting => setting.Include(p => p.Comments.ThenInclude(c => c.Votes)));

The other alternative is for it to always insert the whole object graph automatically. Like the DbContext does today.

EFBatchOperation.For(db, db.BlogPosts).InsertAll(posts);
//Magic happens and everything is saved

So what does people prefer? Would you like to be able to control exactly what goes in or would you prefer the automagic solution?

Some notes:

ErikEJ commented 9 years ago

How about renaming tol InsertGraph and leave InsertAll to jusf insert the top level?

MikaelEliasson commented 9 years ago

I like the it! Would you prefer to have control over which child collections that get inserted?