Nick Brown
Nick Brown

Reputation: 1167

Creating seed model from data already in DB

Is there a way to convert data in an existing database into objects that can easily be put into a seed method?

Essentially I'd like to let some people add lines to my DB via a form and then convert that into an object that I can re-seed the DB anytime I need to make changes.

The database itself is created via code-first using the following model:

public class Combo
{
    public int Id { get; set; }
    public string MainPrefix { get; set; }
    public string MainDescriptor { get; set; }
    public string MainDish { get; set; }
    public string Connector { get; set; }
    public string SecondaryDescriptor { get; set; }
    public string SecondaryDish { get; set; }
}

Upvotes: 2

Views: 253

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

High level untested idea:

Create a new T4 template which will use either your context or direct SQL to query database and generate either C# static class with method (which will return collection of object created from database data) or SQL insert commands. Depending on what you choose you will either call static method in Seed to get all entities you want to insert or simply load the created SQL script and execute it.

You can also do the same without T4 by using Code Dom (if you want to generate C# code) and creating a new custom tool for visual studio.

These features can be part of your project or the result (C# code) can be compiled in separate external assembly.

Upvotes: 2

Related Questions