Seed from Production feature - JonPSmith/EfCore.TestSupport GitHub Wiki
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:
- Extract stage which:
- Disconnects the data from the production database, i.e. resets all primary and foreign keys
- Provides a optional anonymize stage, where you can replace personal data and replace with random data.
- Saves the data to a JSON file in the
TestData
directory in your unit tests.
- Seed database stage which:
- Recreates the Ef Core database classes with their relationships.
- An optional step where you can apply any changes to the data for this specific unit test.
- Used EF Core's Add/AddRange methods to create new entries and then call SaveChanges.
The diagram below show these steps.
Here is a list of situations where I have used this type of seeding before:
- Where the database contains data that is hard to recreate, e.g. data that involves complex calculations like financial calculations or modelling.
- 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.
- Where you want some data for a system testing/client demo, e.g. you need data that looks real.
- Where you want data for a performance testing, e.g. you need data that is typical in your production system.