0

The tagged duplicate question uses a custom context, I'm using the default ApplicationDbContext.

I'm confused why data isn't seeded whenever I run Update-Database. I read the other questions with answers but unfortunately they're using a custom Context, I'm using the default ApplicationDbContext. They seem to have the same answers but they don't work on my end.

I have the following done;

I have a custom Initializer:

public class PostInitializer : DropCreateDatabaseAlways<ApplicationDbContext>
{
        protected override void Seed(ApplicationDbContext db)
        {
            var posts = new List<Post>
            {
                new Post()
                {
                    Content = "TestA"
                },
                new Post()
                {
                    Content = "TestB"
                }
            };

            posts.ForEach(p => db.Posts.Add(p));
            db.SaveChanges();
        }
}

In My Global Asax:

public class MvcApplication : System.Web.HttpApplication
{
        protected void Application_Start()
        {
            // call PostInitializer here
            Database.SetInitializer(new PostInitializer());

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
}

My Model:

public class Post
{
     [Key]
     public int PostId { get; set; }
     public string Title { get; set; }
     public string Content { get; set; }
     public DateTime? DateTimeCreated { get; set; }
}

I've tried deleting the DB and running Update-Database after, however data still isn't seeded.

Jerdine Sabio
  • 5,688
  • 2
  • 11
  • 23
  • I think somewhere in your code you have to call the PostInitializer, which normally is in the context you use. – Babak Fakhriloo Apr 09 '19 at 08:10
  • @BabakFakhriloo I see. I placed it in the Global.asax.cs on Application_Start() method. Well, at least those are the guides that I saw online. – Jerdine Sabio Apr 09 '19 at 08:12
  • 1
    Possible duplicate of [DropCreateDatabaseAlways Seed not called](https://stackoverflow.com/questions/14242699/dropcreatedatabasealways-seed-not-called) – Marco Apr 09 '19 at 08:20

2 Answers2

1

Write your PostInitializer as follows:

public static class PostInitializer
{
    public static void Seed()
    {
        using (ApplicationDbContext dbContext = new ApplicationDbContext())
        {
            if (dbContext.Database.Exists())
            {
                if(!dbContext.Posts.Any())
                {
                    var posts = new List<Post>
                    {
                        new Post()
                        {
                            Content = "TestA"
                        },
                        new Post()
                        {
                             Content = "TestB"
                        }
                    };

                    posts.ForEach(p => dbContext.Posts.Add(p));
                    dbContext.SaveChanges();
                }
            }
        }
    }
}

Then register it as follows:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {

        PostInitializer.Seed(); // <-- Here it is

        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}
TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
  • Thank you, this one does works fine. There's also seeding that could be done on Configuration.cs. It's just that initializers that I was taught were derived from the ones mentioned here; https://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx – Jerdine Sabio Apr 09 '19 at 13:37
  • But difference between the two is that my Seed method will be called as soon as the application start whereas other is not! – TanvirArjel Apr 09 '19 at 13:40
0

Ok my bad. What solved this was running the application. I thought the seeding would happen during Update-Database. This is what worked for me;

  1. Run App
  2. Access Controller with the Model concerned in Initializer, accessing at least the Index page worked for me (localhost/posts)
  3. During Access, the Database will be dropped and created, then seeded with whatever specified in the Initializer
  4. Close any SQL tabs on Visual Studio that is connected to the target DB as it might throw an error during Drop Database
Jerdine Sabio
  • 5,688
  • 2
  • 11
  • 23