I'm attempting code first development on my MVC site and although the global.ascx file is running the code to make a new initializer, the initializer itself isn't being run. I've placed a breakpoint on both the start of the context and the initializer and both are not being hit.
Global.ascx
Database.SetInitializer<AbstractContext>(new AbstractContextInitializer());
Context:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace AbstractThinking2015.Models
{
public class AbstractContext : DbContext
{
public DbSet<BlogModel> Blogs { get; set; }
}
}
Initializer:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace AbstractThinking2015.Models
{
public class AbstractContextInitializer : DropCreateDatabaseAlways<AbstractContext>
{
protected override void Seed(AbstractContext context)
{
context.Blogs.Add(
new BlogModel() { Title = "Test Title",
Date = DateTime.Now,
User = "Liane",
Category = "Test",
Post = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus rhoncus tristique leo et vehicula. Integer eu imperdiet orci, sit amet convallis ipsum. Sed in commodo urna. Sed venenatis neque augue, et faucibus purus aliquet eget. Suspendisse nec sapien nec justo ullamcorper rutrum. Praesent maximus nulla eget rhoncus scelerisque. Vivamus at felis porta, placerat augue non, interdum dui."
});
context.SaveChanges();
}
}
}
And just in case, here's the model:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace AbstractThinking2015.Models
{
public class BlogModel
{
//public BlogModel()
//{
// Date = DateTime.Now;
// User = "Liane";
//}
public int BlogModelId { get; set; }
[DisplayName("Blog Title")]
[Required(ErrorMessage = "Your blog must have a title")]
public string Title { get; set; }
[DisplayName("Blog Date")]
[Required(ErrorMessage = "Your blog must have a date")]
public DateTime Date { get; set; }
[DisplayName("Blog User")]
[Required(ErrorMessage = "Your blog must have a user")]
public string User { get; set; }
[DisplayName("Blog Category")]
[Required(ErrorMessage = "Your blog must have a category")]
public string Category { get; set; }
[DisplayName("Blog Post")]
[Required(ErrorMessage = "Your blog must have a post")]
[DataType(DataType.MultilineText)]
public string Post { get; set; }
}
}