Translate

sábado, 1 de febrero de 2014

How to seed data in a "manual mode" way into an Entity Framework Code First data base

First of all, you need to enable automatic migrations: go to Tools, Library Package Manager and finally to Package Manager Console.

Execute the command: Enable-Migrations. A new class called Configuration appears under a new folder called Migrations. Go to this class and use the following code as an example to seed two tables called Sport and RaceType:




The first method, Seed which is overrided to use our own seed actions includes a call to an SQL Command, in order to delete the tables, and initialize the index counters. Following to a call to the method  GetSports, which obtains data from the Sports List created bellow. For each element on this list we call the Add method.

To manually launch this seed you only need to use the command Update-Database in the previous shell.


No hay comentarios: