我的身份表一直在填充,但后来我意识到我创建了两个独立的数据库,一个基于身份模型(ASP.NET默认),另一个基于我的自定义模型。然后我将两者合并为一个数据库,这就是我的身份表停止填充的时候。
当我运行应用程序并注册用户时,应该使用新用户填充数据库。所以我的表被创建了,但数据(用户,角色等)没有填充。
这是我的上下文类:
public class FlavorPingContext : IdentityDbContext
{
public FlavorPingContext() : base("name=FlavorPingContext")
{
}
public System.Data.Entity.DbSet<FlavorPing.Models.Merchant> Merchants { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.MenuItem> MenuItems { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.MerchantDetails> MerchantDetails { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.Follower> Followers { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.FollowerMenuItemMerchant> FollowerMenuItemMerchants { get; set; }
public DbSet<IdentityUserLogin> UserLogins { get; set; }
public DbSet<IdentityUserClaim> UserClaims { get; set; }
public DbSet<IdentityUserRole> UserRoles { get; set; }
public DbSet<IdentityRole> Role { get; set; }
public virtual DbSet<ApplicationUser> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder builder) {
// Primary keys
builder.Entity<Follower>().HasKey(q => q.FollowerID);
builder.Entity<MenuItem>().HasKey(q => q.MenuItemID);
builder.Entity<Merchant>().HasKey(q => q.MerchantID);
builder.Entity<FollowerMenuItemMerchant>().HasKey(q =>
new {
q.FollowerID, q.MenuItemID, q.MerchantID
});
// Relationships
builder.Entity<FollowerMenuItemMerchant>()
.HasRequired(t => t.Follower)
.WithMany(t => t.FollowerMenuItemMerchants)
.HasForeignKey(t => t.FollowerID);
builder.Entity<FollowerMenuItemMerchant>()
.HasRequired(t => t.MenuItem)
.WithMany(t => t.FollowerMenuItemMerchants)
.HasForeignKey(t => t.MenuItemID);
builder.Entity<FollowerMenuItemMerchant>()
.HasRequired(t => t.Merchant)
.WithMany(t => t.FollowerMenuItemMerchants)
.HasForeignKey(t => t.MerchantID);
builder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
builder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
builder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
//removes plural naming in DB tables
builder.Conventions.Remove<PluralizingTableNameConvention>();
builder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
//This is suppposed to populate Identity tables.
builder.Entity<IdentityUser>().ToTable("User");
builder.Entity<ApplicationUser>().Property(u => u.PasswordHash).HasMaxLength(500);
builder.Entity<ApplicationUser>().Property(u => u.SecurityStamp).HasMaxLength(500);
builder.Entity<ApplicationUser>().Property(u => u.PhoneNumber).HasMaxLength(50);
builder.Entity<IdentityUser>().ToTable("Role");
builder.Entity<IdentityUserRole>().ToTable("UserRole");
builder.Entity<IdentityUserLogin>().ToTable("UserLogin");
builder.Entity<IdentityUserClaim>().ToTable("UserClaim");
builder.Entity<IdentityUserClaim>().Property(u => u.ClaimType).HasMaxLength(150);
builder.Entity<IdentityUserClaim>().Property(u => u.ClaimValue).HasMaxLength(500);
}
}
这是我的创建商家方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "MerchantID,MerchantName,email,website")] Merchant merchant)
{
if (ModelState.IsValid){
merchant.ApplicationUserId = User.Identity.GetUserId();
db.Merchants.Add(merchant);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(merchant);
}
这是我的注册
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
ViewBag.Link = callbackUrl;
//Assign Role to user Here
await this.UserManager.AddToRoleAsync(user.Id, model.role);
//Ends Here
return View("DisplayEmail");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}