Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Database Seeding Framework - C#

RSS
Modified on Thu, Aug 02, 2018, 9:10 AM by Administrator Categorized as ┬ĚNet Framework

Overview

This article presents a means of implementing

Implementation

(1) Console Application Code

static void Main(string[] args)
{
    SeedEngine.ApplyVersionedUpdates();
}

(2) Write Seed Version classes

These should follow a naming convention "Seed_YYYYMMDD_hhmm", which will cause them to be executed in the order in which they were created.

Re-usable Code

public abstract class SeedBase
{
    public abstract void Execute();
}

public class SeedEngine
{
    public static void ApplyVersionedUpdates()
    {
        // TODO: Create a SeedVersion table
        var asm = Assembly.GetExecutingAssembly();

        // TODO: Query the most recent seed version run against the database, and filter
        // the next statement for everything newer than that
        var seedVersions = asm.DefinedTypes.Where(a => a.BaseType == typeof(SeedBase)).OrderBy(a => a.Name);

        foreach (var seedVer in seedVersions)
        {
            var result = Activator.CreateInstance(seedVer) as SeedBase;

            if (result == null)
                continue;

            Debug.Print($"Running seed {seedVer.Name}");
            result.Execute(); 
            // TODO: Insert a SeedVersion record with the current {seedVer.Name}
        }

    }
        
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2018, Patrick Jasinski.