Created
June 15, 2015 04:09
-
-
Save MAOliver/5fa6bbf0616a148a3279 to your computer and use it in GitHub Desktop.
Bulk load to sql server from JSON using EF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using Newtonsoft.Json.Linq; | |
namespace JsonToEntity | |
{ | |
public static class EntityDbLoader | |
{ | |
public static int SaveJsonToDatabase<T>( this IDbContext dbContext, string jsonPath, Func<dynamic, T> mappingFunction, int? batchSize = 200 ) where T : class | |
{ | |
var jsonFile = new FileInfo(jsonPath); | |
var i = 0; | |
var numberOfRecordsSaved = 0; | |
using (var reader = new StreamReader(jsonFile.OpenRead())) | |
{ | |
while (!reader.EndOfStream) | |
{ | |
dynamic jsonObject = JObject.Parse(reader.ReadLine()); | |
T entity = mappingFunction.Invoke(jsonObject); | |
dbContext.Set<T>().Add(entity); | |
if (++i == batchSize.GetValueOrDefault(200)) | |
{ | |
i = 0; | |
numberOfRecordsSaved += dbContext.SaveChanges(); | |
} | |
} | |
} | |
return numberOfRecordsSaved; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment