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.