Skip to content

Instantly share code, notes, and snippets.

@harr1424
Created November 9, 2023 23:52
Show Gist options
  • Save harr1424/1351f114bd4e26c0705809a63842de83 to your computer and use it in GitHub Desktop.
Save harr1424/1351f114bd4e26c0705809a63842de83 to your computer and use it in GitHub Desktop.
Programmatically Seeding Large Amounts of Data using EFCore 7.0
using System.Reflection;
using Blog.Models;
using Newtonsoft.Json;
namespace Blog.Data;
public class DbSeeder
{
private readonly PostContext _postContext;
public DbSeeder(PostContext postContext)
{
_postContext = postContext;
}
public void SeedData()
{
_postContext.Database.EnsureCreated();
var hasData = _postContext.Post.FirstOrDefault();
if (hasData == null)
{
var posts = new List<Post>();
Assembly asm = Assembly.GetExecutingAssembly();
var allFileNames = asm.GetManifestResourceNames();
using (Stream resStream = asm.GetManifestResourceStream(allFileNames[0]))
{
if (resStream == null)
{
Console.WriteLine($"\nResource was found to be null!\n");
return;
}
using (StreamReader r = new StreamReader(resStream))
{
string json = r.ReadToEnd();
posts = JsonConvert.DeserializeObject<List<Post>>(json);
}
}
foreach (var post in posts)
{
_postContext.Post.Add(post);
}
_postContext.SaveChanges();
Console.WriteLine($"Seeded {posts.Count} posts to the database.");
}
}
}
/*
* This class is a model class containing properties to describe a blog post.
* The properties defined in this class map to an array of JSON objects describing
* thousands of blog posts.
*
* In the JSON file I am using, some post id's are null, and so a post's title
* has been used as a key instead.
*/
using System.ComponentModel.DataAnnotations;
namespace Blog.Models;
public class Post
{
public int? Id { get; set; }
[Key]
public string Title { get; set; }
public string Content { get; set; }
public string Url { get; set; }
}
using Microsoft.EntityFrameworkCore;
public class PostContext : DbContext
{
public PostContext (DbContextOptions<PostContext> options)
: base(options)
{
}
public DbSet<Blog.Models.Post> Post { get; set; } = default!;
}
using Blog.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDbContext<PostContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("PostContext") ?? throw new InvalidOperationException("Connection string 'PostContext' not found.")));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
using (var scope = app.Services.CreateScope())
{
var serviceProvider = scope.ServiceProvider;
var postContext = serviceProvider.GetRequiredService<PostContext>();
var dbSeeder = new DbSeeder(postContext);
dbSeeder.SeedData();
}
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment