Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere to put Seed data using EF5 code-first migrations
    primarykey
    data
    text
    <p>I'm starting out with EF and MVC, and following different tutorials provide different confusing ideas on where is the correct place to put dummy test data in my database.</p> <p>So I've made some POCO classes, I have a BlahContext</p> <pre><code>public class BlahContext : DbContext { public DbSet&lt;Blah&gt; Blahs { get; set; } public BlahContext() : base("DefaultConnection") { Configuration.ProxyCreationEnabled = false; } } </code></pre> <p>Then in Global.asax.cs I initialize the database like this:</p> <pre><code>protected void Application_Start() { Database.SetInitializer(new DropCreateDatabaseAlways&lt;BlahContext&gt;()); } </code></pre> <p>Now I did the following in Package Manager Console:</p> <pre><code>PM&gt; enable-migrations -contexttypename Blah.Models.BlahContext PM&gt; add-migration Initial PM&gt; update-database </code></pre> <p>This created Configuration.cs, and in that class there is a Seed() method - the generated comments imply that this is the correct place to put seed data. So I filled it in like this:</p> <pre><code>internal sealed class Configuration : DbMigrationsConfiguration&lt;Blah.Models.BlahContext&gt; { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(Blah.Models.BlahContext context) { #if DEBUG context.Blahs.AddOrUpdate(b =&gt; b.ID, new Blah() { ID = 1, Name = "bum", }, new Blah() { ID = 2, Name = "moo", } ); #endif } } </code></pre> <p>Now when I run <code>update-database</code> again, it populates the Blah table with the 2 records "bum" and "moo". All good.</p> <p>But when I run the application, it seems the <code>Database.SetInitializer()</code> call in <code>Application_Start</code> causes the database to be recreated, but this time the <code>Configuration.Seed()</code> method is <strong>not</strong> called. So the data is no longer present.</p> <p>I saw some other tutorials which created a class such as:</p> <pre><code>class BlahContextInitializer : DropCreateDatabaseAlways&lt;BlahContext&gt; </code></pre> <p>and then Application_Start looks like:</p> <pre><code>protected void Application_Start() { Database.SetInitializer(new BlahContextInitializer()); } </code></pre> <p>And then in the <code>BlahContextInitializer.Seed()</code> method that's where the dummy data is contained. Is this strategy for EF4.1 only?</p> <p>Where should I put my seed data in EF5 so that it doesn't get wiped out when the application starts?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload