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 clear example (why doesn't it come out of the box?) of HOW I can seed the Identity 2.0 database and all examples I see are half backed because they don't say WHERE exactly you have to implement that.
I used enable-migrations which created a Migrations folder with a Configuration.cs file. It has a Seed method override but when I place a breakpoint it notice it is never executed, in fact the Identity database is not even populated with the schema either.
So where and what I have to do so that the Identity 2.0 schema is created on the database the first time (the connection string is correct and the empty database exists). And how do I rig up the Seeding?
On IdentityModels.cs I have this: public class ApplicationDbContext : IdentityDbContext { ublic ApplicationDbContext(): base("DefaultConnection", throwIfV1Schema: false) { }
public static ApplicationDbContext Create() {
return new ApplicationDbContext();
}
protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
// to avoid the "has no keys" errors when running Update-Database on PM
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id).ToTable("AspNetRoles");
modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");
modelBuilder.Entity<IdentityUserLogin>().HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey }).ToTable("AspNetUserLogins");
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }).ToTable("AspNetUserRoles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
}
}
In Migrations/Configuration.cs (added by PM>Enable-Migrations) I have this:
internal sealed class Configuration : DbMigrationsConfiguration<Models.ApplicationDbContext> {
public Configuration() {
AutomaticMigrationsEnabled = false;
}
protected override void Seed(Models.ApplicationDbContext context) {
WriteReferenceData();
}
}
In my Global.asax.cs file on the Application_Start() method I added this:
System.Data.Entity.Database.SetInitializer<Models.ApplicationDbContext>(new System.Data.Entity.MigrateDatabaseToLatestVersion<Models.ApplicationDbContext, Migrations.Configuration>());
And in IdentityConfig.cs I have this DB Initializer as well though it seems to be orphan because I don't know where to plug this in:
public class ApplicationDbInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<Models.ApplicationDbContext> {
protected override void Seed(ApplicationDbContext context) {
WriteReferenceData();
base.Seed(context);
}
}
And finally the WriteReferenceData method is in some other class that does this more or less:
System.Data.Entity.DbContextTransaction transaction = null;
try {
System.Data.Entity.DbContext ctx = Models.ApplicationDbContext.Create();
transaction = ctx.Database.BeginTransaction();
CreateRoles(ctx);
CreateUsers(ctx);
CreateRoleAssociations(ctx);
ctx.SaveChanges();
transaction.Commit();
succeeded = true;
}
catch (Exception ex) {
if (transaction != null { transaction.Rollback(); transaction.Dispose(); }
succeeed = false;
}
return succeeded;