Skip to content

Instantly share code, notes, and snippets.

@MAOliver
Created June 15, 2015 04:09
Show Gist options
  • Save MAOliver/5fa6bbf0616a148a3279 to your computer and use it in GitHub Desktop.
Save MAOliver/5fa6bbf0616a148a3279 to your computer and use it in GitHub Desktop.
Bulk load to sql server from JSON using EF
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