0

My issue is that when I run my web app (and delete the DB) my database initializer works perfectly, however when I enter Add-Migration Initial & Update-Database, only the database is create without the initialization data !!

I have the following code: (This is in PROJ1.EF Project)

public class StudentsContext : DbContext
    {
        public virtual DbSet<Student> Students { get; set; }

        public StudentsContext ()
            : base("StudentsContext")
        {
            Database.SetInitializer<StudentsContext>(new StudentsContextInitializer());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

The following is my StudentsContextInitializer:

public class StudentsContextInitializer : DropCreateDatabaseAlways<StudentsContext>
{
        protected override void Seed(StudentsContext context)
        {
            var students = new List<Student>
            {
                new Student("Alex ABC"),
                new Student("John Doe")
            };

            students.ForEach(s => context.Students.Add(s));
            context.SaveChanges();

            base.Seed(context);
        }
}

My web.config has the following entries in its <entityFramework> tag: (This is in PROJ1.Web Project)

<contexts>
  <context type="PROJ1.EF.StudentContext, PROJ1.EF">
    <databaseInitializer type="PROJ1.EF.StudentsInitializer, PROJ1.EF" />
  </context>
</contexts>

Please help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
AMH
  • 1
  • I think your `Database.SetInitializer` call is too late. Try putting it in a static constructor. i.e. `static StudentsContext() { /* set initializer */ }` – ta.speot.is Feb 28 '15 at 08:25
  • And for what it's worth, `` refers to `StudentsInitializer` but your class is called `StudentsContextInitializer` – ta.speot.is Feb 28 '15 at 08:27
  • 1
    And for what it's worth as well, what's the point of `Add-Migration Initial` and `Update-Database` for something that's based on `DropCreateDatabaseAlways`. Seems like migrations are mutually exclusive, here. – ta.speot.is Feb 28 '15 at 08:28
  • ta.speot.is in right. When you run Enable-Migrations a new class is added to your project; Configuration : DbMigrationsConfiguration and this is the one that has the Seed method that will be executed when you run the migrations (Update-Database). – Augusto Barreto Feb 28 '15 at 10:00
  • Thanks ta.speot & Augusto ,, I think the main issue is that DbMigrationsConfiguration creates new Seed method which is empty ,, how to integrate this with my Initializer class as I don't want to put the same code in this new Seed method (any new Add-Migration would create new Seed method right??) – AMH Feb 28 '15 at 13:21
  • @AMH No, it won't create a new Seed method. You'll only have one DbMigrationsConfiguration. You could create a separate class (ex: InitialSeed) and use it from all places, but if you are using migrations then there is no point in using DropCreateDatabaseAlways. You'll have to use MigrateToLatestVersion initializer. Please, read this article written by Arthur Vickers from the EF team: http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/. It'll help you understand this situation ;). – Augusto Barreto Feb 28 '15 at 13:28

0 Answers0