解决方案 2:保留两个系统,只需在“AspNetUsers”和“MyOldUsersTable”之间添加 PK-FK。可能,但字段(电子邮件、电话)中可能存在重复项。另外,要维护两个用户表也并不理想。
当在表之间添加一对零对一的关系时,它会起作用,如下所示:
modelBuilder.Entity<MyOldUsersTable>()
.HasOptional(e => e.ApplicationUser)
.WithRequired(e => e.MyOldUsersTable)
.Map(m => m.MapKey("MyOldUsersTableId"))
.WillCascadeOnDelete(false);
最佳:解决方案 3。通过删除来自 IdentityUser 的所有默认行为,使 Identity 了解用户表现在是“MyOldUsersTable”。
第一步是通过替换以下内容来删除对 IdentityDbContext 的引用:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
by:
public class ApplicationDbContext : DbContext
然后将默认的 UserStore 替换为自定义的 UserStore:
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
取而代之:
var manager = new ApplicationUserManager(new CustomUserStore(context.Get<ApplicationDbContext>()));
最后,使用所需的接口来实现 CustomUserStore。例如密码、电子邮件和安全标记:
public class CustomUserStore : IUserStore<MyOldUsersTable, int>, IUserPasswordStore<MyOldUsersTable, int>, IUserEmailStore<MyOldUsersTable, int>, IUserSecurityStampStore<MyOldUsersTable, int>