7

What is the main puspose of seed method in the migration folder of my application? In my Configuration.cs file I got this in my seed method -

protected override void Seed(TestApplication.DataBaseContext.AppDBContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
            SeedMemebership();
        }

        private void SeedMemebership()
        {
            if (!WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("DefaultConnection",
                    "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
            var roles = (SimpleRoleProvider)Roles.Provider;
            var membership = (SimpleMembershipProvider)Membership.Provider;

            if (!roles.RoleExists("Administrator"))
            {
                roles.CreateRole("Administrator");
            }
            if (membership.GetUser("admin", false) == null)
            {
                membership.CreateUserAndAccount("admin", "password");
            }
            if (!roles.GetRolesForUser("admin").Contains("Administrator"))
            {
                roles.AddUsersToRoles(new[] { "admin" }, new[] { "Administrator" });
            }
        }

As anyone can make out it calls the SeedMembership() which creates a role and a user if it does not exist. When is this seed() called and what does it do? I tried putting a break point on this method but it never really got hit. I tried searching other SO questions for further explainations but it dint help.

Thank You.

Aritra B
  • 1,726
  • 6
  • 29
  • 45
  • 1
    This helps? http://stackoverflow.com/questions/20245527/entity-framework-seed-method-is-not-being-called – HOKBONG Feb 20 '14 at 07:04

2 Answers2

7

This seed() method in configuration.cs is called when you run update-database in the Package Manager Console.

It's also called at application startup if you change Entity Framework to use the MigrateDatabaseToLatestVersion database initializer.

Anthony Chu
  • 37,170
  • 10
  • 81
  • 71
  • So why does not it hit the break point when you run `Update -database` command from PMC? – Aritra B Feb 20 '14 at 07:13
  • 1
    It doesn't run it with the debugger attached. Try the solution in this question... http://stackoverflow.com/questions/16718510/debugging-package-manager-console-update-database-seed-method – Anthony Chu Feb 20 '14 at 07:23
1

The migrations feature of the Entity Framework includes a Seed method where you can populate the database with the initial static data an application needs.

more information

Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110