Using Mockaroo and Entity framework to Seed Sample Data for .NET Applications
Source: Dev.to
Introduction
Testing your applications thoroughly requires diverse sample data, which can be time‑consuming to create manually. Mockaroo simplifies this process by generating realistic test data quickly. In this tutorial we’ll use Mockaroo together with Entity Framework Core to seed sample data for a .NET Web API project.
You’ll need Visual Studio Community Edition, which you can download here.
Create a Dataset in Mockaroo
- Navigate to the Mockaroo website.
- Define the dataset fields (example screenshot below).

- Select JSON as the output format (since we’ll seed the data through EF Core).
- Click GENERATE DATA and download the generated file (e.g.,
MOCK_DATA.json).
Create a Web API Project
- Open Visual Studio → Create a new project.
- Search for ASP.NET Core Web API, select it, and click Next.
- Name the project (e.g.,
Mockaroo_Test), choose .NET 10 as the target framework, keep the default options, and click Create.

Install Required Packages
Open the Package Manager Console and run:
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.InMemory
Install-Package Microsoft.EntityFrameworkCore.Design
Define the Model
Create a Club class that matches the fields defined in Mockaroo:
using System.Text.Json.Serialization;
public class Club
{
public int Id { get; set; }
[JsonPropertyName("club_name")]
public string ClubName { get; set; }
[JsonPropertyName("country_location")]
public string CountryLocation { get; set; }
[JsonPropertyName("stadium_capacity")]
public int StadiumCapacity { get; set; }
[JsonPropertyName("manager_name")]
public string ManagerName { get; set; }
[JsonPropertyName("founded_year")]
public int FoundedYear { get; set; }
[JsonPropertyName("league_affiliation")]
public string LeagueAffiliation { get; set; }
[JsonPropertyName("captain_name")]
public string CaptainName { get; set; }
[JsonPropertyName("jersey_color")]
public string JerseyColor { get; set; }
[JsonPropertyName("average_attendance")]
public int AverageAttendance { get; set; }
[JsonPropertyName("major_rival")]
public string MajorRival { get; set; }
}
Create the DbContext
Add a new class AppDbContext:
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions options) : base(options) { }
public DbSet Clubs { get; set; }
}
Configure In‑Memory Database
In Program.cs, register the context:
builder.Services.AddDbContext(options =>
{
options.UseInMemoryDatabase("MockarooDemoDb");
});
Seeding the Data
-
Move the downloaded
MOCK_DATA.jsonfile into the project folder (e.g., the root of the Web API project).

-
Create a
SeedDataclass to load the JSON and populate the database:
using System.Text.Json;
public class SeedData
{
public static async Task SeedAsync(AppDbContext context)
{
if (context.Clubs.Any()) return;
var json = await File.ReadAllTextAsync("MOCK_DATA.json");
var clubs = JsonSerializer.Deserialize>(json);
context.Clubs.AddRange(clubs!);
await context.SaveChangesAsync();
}
}
- Invoke the seeding logic from
Program.csafter building the app:
var app = builder.Build();
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await SeedData.SeedAsync(db);
}
Now, when you run the application, the in‑memory database MockarooDemoDb will be populated with the mock data.
Visualize the Seeded Data
Create a controller to expose the clubs via an API endpoint.
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
[ApiController]
[Route("api/[controller]")]
public class ClubController : ControllerBase
{
private readonly AppDbContext _context;
public ClubController(AppDbContext context)
{
_context = context;
}
[HttpGet]
public async Task>> Get()
{
var clubs = await _context.Clubs.ToListAsync();
return Ok(clubs);
}
}
Swagger
If you don’t already have Swagger set up, follow the guide here.
Run the project and navigate to the Swagger UI page. Executing the GET /api/Club endpoint will display the seeded club data.

Full Project Source
The complete project is available on GitHub:
Happy coding!