Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to seed data using EntityFramework Code first Migrations

Hi I'm using the Beta 1 version of this nuGet package, the database is allready created and I need to know if there is a way to populate my tables through migrations. Thanxs

like image 306
Guillermo Oramas R. Avatar asked Dec 09 '11 15:12

Guillermo Oramas R.


People also ask

What are the migration commands we use with code First approach in Entityframework?

Now it's time to use the Code First Migration approach. Open Package Manager Console. Run Enable-Migrations command in a Package Manager console. This command added two more classes to your project in the Migrations folder.


1 Answers

The intro post shows how to seed data http://blogs.msdn.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-no-magic-walkthrough.aspx

Seed data: Override the Seed method in this class to add seed data. - The Seed method will be called after migrating to the latest version. - You can use the DbContext.AddOrUpdate() helper extension method to avoid creating duplicate seed data. E.g.

myContext.AddOrUpdate(c => c.FullName,
  new Customer { FullName = "Andrew Peters", CustomerNumber = 123 },
);
like image 179
Betty Avatar answered Nov 01 '22 18:11

Betty