Why seeding test data into database using CherrySeed (.NET C#)

Michael Altmann
2 min readSep 27, 2016

--

I think each developer knows that situation — you start with a new software project and you need some data for tests or demos in the database. So you choose the quickest and dirtiest solution: Hardcode the test data in .NET source code and seed it with your O/R mapping framework into the database. This code is messy and hard to maintain. As one form of improvement you have to move your hardcoded test data into well known and easily human-readable data formats (e.g. CSV, XML, JSON, Gherkin etc.) and write your own seeding logic which inserts data into the database, handles primary keys correctly and brings relations between entities under control.

CherrySeed

As .NET developer you have the possibility to use CherrySeed, a small .NET library which provides a flexible way to seed test data into the database. CherrySeed contains the complex logic with features to bring relations between entities under control, handle primary keys and many more to suit your needs. CherrySeed has a plugin-based design without any dependencies. So it is easily extendable and doesn’t force you to use a specific data format (CSV, XML, JSON, Gherkin etc.) or a specific O/R mapping framework — you keep the control.

Because CSV is very popular in describing test data there is already an existing CherrySeed extension which loads test data from CSV files. Gherkin is also a good way to describe test data. That’s why CherrySeed provides an extension which loads test data from Gherkin files. Entity Framework is the most popular O/R mapping framework in .NET, so there is also a ready-to-use CherrySeed extension to seed test data into the database using Entity Framework. But it is very easy to create your own extension, for example to load test data from JSON files or to seed data via NHibernate.

You can find more info about CherrySeed and their extensions in the official github repository in the wiki.

--

--