Using Mockaroo and Entity framework to Seed Sample Data for .NET Applications

Published: (December 15, 2025 at 08:38 AM EST)
3 min read
Source: Dev.to

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

  1. Navigate to the Mockaroo website.
  2. Define the dataset fields (example screenshot below).
    Setting up Mockaroo
  3. Select JSON as the output format (since we’ll seed the data through EF Core).
  4. Click GENERATE DATA and download the generated file (e.g., MOCK_DATA.json).

Create a Web API Project

  1. Open Visual Studio → Create a new project.
  2. Search for ASP.NET Core Web API, select it, and click Next.
  3. Name the project (e.g., Mockaroo_Test), choose .NET 10 as the target framework, keep the default options, and click Create.
    Setting up Web API project

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

  1. Move the downloaded MOCK_DATA.json file into the project folder (e.g., the root of the Web API project).
    Move Downloaded Mockaroo file to application folder

  2. Create a SeedData class 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();
    }
}
  1. Invoke the seeding logic from Program.cs after 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.
Visualizing the seeded data

Full Project Source

The complete project is available on GitHub:

Happy coding!

Back to Blog

Related posts

Read more »