1

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; }
    }
}
Graphican0
  • 167
  • 11
Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • Did you change the model? The initializer only runs when the model changes. Try running the command var modelChanged = context.Database.CompatibleWithModel(true); and see if the model changed... – Steve Greene Jan 07 '16 at 21:16

3 Answers3

1

You can put the call to the initializer inside the DbContext class, like so:

Public Class AuctionsDataContext
    Inherits DbContext

    Public Property Auctions As DbSet(Of Auction)
    Public Property Bids As DbSet(Of Bid)

    Shared Sub New()
        Database.SetInitializer(New DropCreateDatabaseIfModelChanges(Of AuctionsDataContext))
    End Sub

End Class

Note that the constructor needs to be defined as a static (C#) or shared (VB) method.

James Balentine
  • 212
  • 3
  • 9
0

Be sure to put your initializer code inside the protected void Application_Start() method of MvcApplication class in the Global.asax.cs file.

EduLopez
  • 721
  • 7
  • 18
0

Here is a post about the DropCreateDatabaseAlwaysInitializer. How do I use Entity Framework in Code First Drop-Create mode?

@EduLopez is right, ensure that you run the following

Database.SetInitializer(new AbstractContextInitializer());

before accessing the context.

I'm also not 100% sure, but I think seeding doesn't take place until you try and access the context. So be sure to run a query before thinking it isn't working.

Community
  • 1
  • 1
Blast_dan
  • 1,135
  • 9
  • 18