Creating Random Demo Data with Faker

In my last post I outlined how I setup seed data for an ASP.NET application. Making it easier to create and re-create the initial state for my application data.

Another scenario that I need to frequently address is the need to create larger random datasets across my data model.  This data may simply help fill out the application to make it more realistic looking, aid in developing responsive layouts, or for developing features like paging and reports that require a decent amount of data.

I've taken multiple approaches to creating this data in the past but recently I've been using the Faker.Net library to generate random demo data.

Introducing Faker.Net

The Faker.Net package provides several different types of random data along with some methods to control how the data is generated.

To generate a person with an address you could use the following Faker code.

var person = new Person(){
    FirstName = Faker.Name.First(),
    LastName = Faker.Name.Last(),
    Address = Faker.Address.StreetAddress(),
    City = Faker.Address.City(),
    ZipCode = Faker.Adderss.ZipCode(),
    Company = Faker.Company.Name()    

The library supports the creation of the following types of fake data

addresses (UK, US),
finance (isin, ticker, coupon, maturity, bond name),
identification (social security number (US), MBI (US), national insurance number (UK), passport number (UK & US), Bulgarian Person Identification Number(PIN/ENG))
internet (email, domain names, user names),
lorem ipsum,
phone numbers
Fake data types from Faker.Net GitHub page

Using Faker.Net

Creating test data to exercise paging options is one of my goto usages.  So let's return the Paging Example project and see how to create some demo data.

After cloning the paging example repository we can seed it with initial data and then run it to see what we're starting with.

$ dotnet run /seed

$ dotnet run

Nothing, just an empty list.  

Update the View

To fix this let's add a button to allow the user to create demo data if there is no data available.

@if(Model.Widgets.TotalItems == 0){
    <td colspan="10" class="text-center">
        no widgets available
            <form method="post">
            <button type="submit" class="btn btn-primary">Create Demo Data</button>

We also added a check to prevent the button from showing up when the user has entered a search term that does not return any results.

The Post Handler

To implement the handler for this button we'll add the Faker.Net package and then implement a post handler to generate 100 random widgets every time it is called.

dotnet add package Faker.Net

public async Task<IActionResult> OnPostAsync(){

    for(int i = 0; i < 100; i++){
        var widget = new Widget(){
            Id = Guid.NewGuid(),
            Name = string.Join(" ",Faker.Lorem.Words( (i % 2) + 1)),
            Description = Faker.Lorem.Sentence(),
            PartNumber = Faker.Identification.UsPassportNumber(),
            Quantity = Faker.RandomNumber.Next(0, 10000),
            Price = Faker.Finance.Coupon()

        await _dbContext.Widgets.AddAsync(widget);

    await _dbContext.SaveChangesAsync();

    return RedirectToPage("./Widgets");

For our widgets we made use of the Lorem Ipsum generator for some text and randomly generated some numbers, prices and identification values.  After running this handler our paging view will look something like this.

Pretty cool!  We are in a much better situation to begin testing paging and filtering in our view now and with only a few lines of code.


Unlike seed data, demo data is not required for an application to run smoothly but by using random demo data we can fill out our application and get a leg up on developing more data intensive features.

Other Scenarios

  • Implement a data generator service and wire it just like our seed data with a call to "dotnet run /demodata"
  • Restrict the "Create Data" button and post handler to only user in the "admin" role
  • How can we extend Faker.Net to include domain data specific to our application?
  • Try using another library like Bogus or GenFu
  • How can we extend this data creation to make it easy to remove the demo data when we are done with it? (in the paging example I delete the SQLite database but that's not a good solution long term)

Learn More

Cover image by Bermix Studio on Unsplash