Search code examples
c#entity-frameworkef-code-first

EF Code First Generate C# Seed Method from Database


I have a database that is populated with test data. The table relation is complex and therefore I would like to know if I can generate c# code for inserting that data.

Basically I want to export all that data a C# method Seed that will populate it back when code first migration is run.


Solution

  • In the package management console, enter the following command:

    enable-migrations
    

    It will enable the configuration part of EF and includes a Seed method. If this is not enough there are more options you can explore, like add-migration to add a custom migration.

    See this post for details: http://msdn.microsoft.com/en-us/data/jj554735.aspx or http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/migrations-and-deployment-with-the-entity-framework-in-an-asp-net-mvc-application

    When using a Code Based Migration, you can query for data, remove it and re-insert it any way you like.