Skip to content

Seed from Production feature

Jon P Smith edited this page Oct 20, 2019 · 3 revisions

Overview

NOTE: Read the article Getting better data for unit testing your EF Core applications for a longer explanation.

This library contains a feature that can help you set up realistic test data in your unit tests by taking data from a production database, cleaning it and storing it locally as a JSON file. It consist of two parts:

  1. Extract stage which:
    1. Disconnects the data from the production database, i.e. resets all primary and foreign keys
    2. Provides a optional anonymize stage, where you can replace personal data and replace with random data.
    3. Saves the data to a JSON file in the TestData directory in your unit tests.
  2. Seed database stage which:
    1. Recreates the Ef Core database classes with their relationships.
    2. An optional step where you can apply any changes to the data for this specific unit test.
    3. Used EF Core's Add/AddRange methods to create new entries and then call SaveChanges.

The diagram below show these steps. Seed from Production

Here is a list of situations where I have used this type of seeding before:

  1. Where the database contains data that is hard to recreate, e.g. data that involves complex calculations like financial calculations or modelling.
  2. Where there are lots of large tables involved, e.g. writing code to set up the data would take a lot of effort to write and keep up to date.
  3. Where you want some data for a system testing/client demo, e.g. you need data that looks real.
  4. Where you want data for a performance testing, e.g. you need data that is typical in your production system.

Other documentation pages

  1. Seed from Production - Extract stage
  2. Seed from Production - Anonymization
  3. Seed from Production - Seed database
Clone this wiki locally