Bulk Insert new Entities

Mar 26, 2014 at 9:05 PM
I can't seem to figure out how to do this (maybe I can't). I want to create some new entities and just bulk insert them into a destination (EF modeled) database.

Is that possible?
Coordinator
Apr 6, 2014 at 5:13 PM
Entity Framework ETL was not really designed for this. However, I understand there is a need. I added a extension method for DbContext called SaveBatchedChanges. This will allow you to do batch inserts in a very simple way.
using (var context = new UsersContext())
            {
                IEnumerable<World> worlds = new List<World>();
                context.Worlds.AddRange(worlds);
                CommandCacheInterceptor.PrimaryKeyName = "WorldId"; //optional if your Pk is not Id
                context.SaveBatchedChanges();  
            }
There are a few things you should know. If you are doing any calculated fields besides the PK, you will likely get an exception. After you call SaveBatchedChanges your entites will get updated with a negative PK value. Your data will be inserted correctly but your client side entities will not. I can fix this but I am not sure if it is worth it at this time. If you have a primary key that is not name "Id" then you will have to specify the PrimaryKeyName similar to the code above. The code assumes you have a autoincremented integer primary key. Anything other than this will cause problems. Again this can be modified but I figured this would server most people. Let me know how it goes.

Thanks,
Chris
Apr 7, 2014 at 2:24 PM
OK, well now I suppose I'm pushing the design even further. It doesn't take into account related objects. I have a parent object with some related objects. In code I can only set references of course (Child.Parent = parent). It blows up trying to insert the child without FK to the parent.
Coordinator
May 17, 2014 at 2:27 PM
Try 1.0.8.
May 19, 2014 at 6:43 PM
The 1.0.8 nuget package lacks an Extensions namespace; I downloaded the code and referenced that. When I tried it, I hit the NotImplementedException in EntityFrameworkETL.Interceptors.CommandCacheInterceptor.NonQueryExecuting.
Coordinator
May 24, 2014 at 4:08 AM
I uploaded 1.0.9. I removed the not implemented exceptions from the methods in the interceptor. I did not come across these methods during my tests. I tested with grandparent, parent, child relationship. I am curious if you could give it another try. Can you see what the command text is for the command that was throwing the not implemented exception? These methods should be executed normally for now. The insert commands should still be batched.
May 27, 2014 at 1:07 PM
OK, I'm essentially right back where I was. I have tables with a one-to-one relationship. It's like a Person table with Id as PK. And a PersonMisc table also with Id PK where PersonMisc has FK constraint on it's Id back to Person. We have to have it this way due to large number of columns. I create a Person, PersonMisc, set reference of Person.PersonMisc to the PersonMisc object, call SaveBatchedChanges, and I see that PersonMisc's Id value was not set (violation of FK).