Reputation: 101
I customized Microsoft.AspNet.Identity
classes. The code is below:
public class ApplicationUserRole : IdentityUserRole<Guid>
{
}
public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
}
public class ApplicationUserLogin : IdentityUserLogin<Guid>
{
}
public class ApplicationRole : IdentityRole<Guid, ApplicationUserRole>
{
public ApplicationRole() { }
public ApplicationRole(string name) { Name = name; }
}
public class ApplicationUserStore : UserStore<ApplicationUser, ApplicationRole, Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class ApplicationRoleStore : RoleStore<ApplicationRole, Guid, ApplicationUserRole>
{
public ApplicationRoleStore(ApplicationDbContext context)
: base(context)
{
}
}
public class ApplicationUser : IdentityUser<Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().HasKey(l => l.Id).ToTable("tbl_Users", "membership");
modelBuilder.Entity<ApplicationRole>().HasKey(l => l.Id).ToTable("tbl_Roles", "membership");
modelBuilder.Entity<ApplicationUserClaim>().HasKey(l => l.Id).ToTable("tbl_UserClaims", "membership");
modelBuilder.Entity<ApplicationUserLogin>().HasKey(l => new { l.UserId, l.ProviderKey, l.LoginProvider }).ToTable("tbl_UserLogins", "membership");
modelBuilder.Entity<ApplicationUserRole>().HasKey(l => new { l.RoleId, l.UserId }).ToTable("tbl_UserRoles", "membership");
}
When I run it first, everything works, creating tables in SQL Server; but If I add property (example public string FirstName {get; set;}
) in class ApplicationUser
and run it to make changes in database, throws error:
The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database
I know that I must enable migrations, but is there any way to do it without migrations, because it create own folder(Configuration) and generate class in it?
Upvotes: 0
Views: 1836
Reputation: 6839
Just register this entry at the Global.asax:
protected void Application_Start()
{
//Other calls...
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
}
This will recreate your Identity Database every time that Its model changes. The Context from Identity uses the same concept as any database you create using EF.
You can use other methods, just check here for more information and exemples: System.Data.Entity Namespace
EDIT:
To seed you database everytime It's drops you should implement a Initializer Class
like this one, and override the Seed
method to put some "fake" data:
public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
protected override void Seed(ApplicationDbContext context)
{
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
if (!RoleManager.RoleExists("admim"))
{
var roleresult = RoleManager.Create(new IdentityRole(Administrador));
}
//Create User=Admin with password=123456
var user = new ApplicationUser();
user.UserName = "sysadmin";
var adminresult = UserManager.Create(user, "123456");
//Add User Admin to Role Admin
if (adminresult.Succeeded)
{
var result = UserManager.AddToRole(user.Id, "admin");
}
}
}
Then register this in the Global.asax:
protected void Application_Start()
{
//Other calls...
Database.SetInitializer(new ApplicationDbInitializer());
}
Upvotes: -3