Seed method in MVC entity infrastructure

What is the main purpose of the seed method in the transfer 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. Eg // // 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 soon as anyone can figure this out, he calls SeedMembership() , which creates the role and the user if he does not exist. When is seed() called and what does it do? I tried to set a breakpoint on this method, but it never hit the target. I tried looking for other SO questions for further explanations, but that helps.

Thanks.

+7
c # asp.net-mvc asp.net-mvc-3 entity-framework
source share
2 answers

This seed() method in configuration.cs is called when update-database run in the package manager console.

It is also called at application startup if you modify the Entity Framework to use the MigrateDatabaseToLatestVersion database initializer .

+4
source share

The migration function in the Entity Framework includes the Seed method, in which you can populate the database with the initial static data that applications need.

Additional Information

+1
source share

All Articles