SQL Entity Framework Add to Database Question

Joined
Dec 24, 2019
Messages
10
Location
Florida, USA
Programming Experience
Beginner
Hello, I am using c# and entity frarmework to save data to a database...there will be over 30,000 objects to be saved to the database.....my question is -

is it better to save a single object over and over again 1 at a time or is it more efficient to save a List<Object> with .Range?

thank you
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
Is the saving of these 30,000 objects a one time seeding of the database? Or is this a periodic operation (weekly, monthly, yearly) where you need to load 30,000 new items in a big batch? If it is a one time load, the efficiency doesn't really matter that much since it's a one time cost.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
Also relevant:
 
Joined
Dec 24, 2019
Messages
10
Location
Florida, USA
Programming Experience
Beginner
Hello Skydiver,
this will be a weekly operation but only the first one will be that many objects...the rest will only be "new" items gathered.
 

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
922
Location
Virginia Beach, VA
Programming Experience
10+
See that SO post I linked above. Lots of good info there since I am very anti-EF, and anti-SQL, as well.

My personal recommendation is to do a straight on SqlBulkCopy if you can, and just bypass EF completely. If you must absolutely use EF, then that common wisdom is to do batches of about 500 objects and call SaveChanges() within the lifetime of a DbContext, dispose of that context. Lather, rinse, repeat until you finish uploading your new data.
 
Top Bottom