18

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

1 Answer 1

18

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 },
);
Sign up to request clarification or add additional context in comments.

4 Comments

I was implementing the answer and I noticed that indeed I can write my seeds at the Configuration file, but everytime I use the Update-Database code, those seed are going to populate the DB, everytime! :( ... I try adding the seed on the migrations files instead, but I think it doesnt work that way. Can you help me with this, i just want those seeds populate my db once and not every time i go Update-Database, thanxs pal
You have access to the context at that point, you could easily do wrap a myContext.Tablename.Any() around the code block.
I actually find it's better to create a migration and use Sql("Insert blah...") instead of using AddOrUpdate.
This is a little out-of-date. That post for instance no longer exists. Good answer at stackoverflow.com/questions/9342459/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.