Seeding Identity 2.0 database

前端 未结 5 1573
走了就别回头了
走了就别回头了 2021-02-06 01:00

I have an ASP.NET MVC 5 project (razor engine) which has Identity 2.0 with Individual User Accounts. I am using Visual Studio Professional 2013

I have not found any clea

5条回答
  •  后悔当初
    2021-02-06 01:54

    So we do something similar in our samples package in the following way (To seed the db with our admin user)

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }
    
        static ApplicationDbContext()
        {
            // Set the database intializer which is run once during application start
            // This seeds the database with admin user credentials and admin role
            Database.SetInitializer(new ApplicationDbInitializer());
        }
    
        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
    
    // This is useful if you do not want to tear down the database each time you run the application.
    // public class ApplicationDbInitializer : DropCreateDatabaseAlways
    // This example shows you how to create a new database if the Model changes
    public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges 
    {
        protected override void Seed(ApplicationDbContext context) {
            DoYourSeedingHere(context);
            base.Seed(context);
        }
    
    }
    

提交回复
热议问题