2

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.

Stefan
  • 17,448
  • 11
  • 60
  • 79
David Dury
  • 5,537
  • 12
  • 56
  • 94

1 Answers1

1

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.

Stefan
  • 17,448
  • 11
  • 60
  • 79
  • 1
    But - how can I get data from database to c# code using migrations? There is not something my data in Migration folder.. Is that even possible? – Alamakanambra Nov 19 '14 at 16:49
  • @Alamakanambra: You can use the basic database functions to generate a script. This can be used by migrations or something else. See this post for MS-SQL: http://stackoverflow.com/a/1515975/2416958 – Stefan Nov 20 '14 at 02:52
  • 1
    Enable-Migrations Add-Migration InitialCreate Update-Database these are the commands I use in dev to get my database seeded – Christopher Douglas Oct 25 '16 at 16:31