我已经使用自定义类实现了Identity 2.1,以使所有实体都具有整数键。
我在迁移文件夹 运行 我的用户商店: 我的用户经理:protected override void Seed(ApplicationDbContext context)
{
var roleManager = new ApplicationRoleManager(new ApplicationRoleStore(context));
var userManager = new ApplicationUserManager(new ApplicationUserStore(context));
if (!roleManager.RoleExists("User"))
{
roleManager.Create(new ApplicationRole("User"));
}
if (!roleManager.RoleExists("Supervisor"))
{
roleManager.Create(new ApplicationRole("Supervisor"));
}
if (!roleManager.RoleExists("Administrator"))
{
roleManager.Create(new ApplicationRole("Administrator"));
}
if (!roleManager.RoleExists("Owner"))
{
roleManager.Create(new ApplicationRole("Owner"));
}
if (!roleManager.RoleExists("System User"))
{
roleManager.Create(new ApplicationRole("System User"));
}
if (userManager.FindByName("h2euser") == null)
{
var h2Euser = new ApplicationUser
{
UserName = "h2euser",
Email = "test"
};
var result = userManager.Create(h2Euser, "Letme1n");
if (result.Succeeded)
{
userManager.AddToRole(h2Euser.Id, "System User");
}
}
if (userManager.FindByName("owner") == null)
{
var ownerUser = new ApplicationUser
{
UserName = "owner",
Email = "test2"
};
var result = userManager.Create(ownerUser, "ChangeMe!");
if (result.Succeeded)
{
userManager.AddToRole(ownerUser.Id, "Owner");
}
}
}
Update-Database
方法时出现此错误:System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ((SELECT CASE WHEN (`Extent2`.`UserId` IS NULL) THEN (NULL) ELSE (1) END' at line 33
public class ApplicationUserStore
: UserStore<ApplicationUser, ApplicationRole, int,
ApplicationUserLogin, ApplicationUserRole,
ApplicationUserClaim>
{
public ApplicationUserStore()
: this(new IdentityDbContext())
{
base.DisposeContext = true;
}
public ApplicationUserStore(DbContext context)
: base(context)
{
}
}
public class ApplicationUserManager : UserManager<ApplicationUser, int>
{
// *** ADD INT TYPE ARGUMENT TO CONSTRUCTOR CALL:
public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
// *** PASS CUSTOM APPLICATION USER STORE AS CONSTRUCTOR ARGUMENT:
var manager = new ApplicationUserManager(
new ApplicationUserStore(context.Get<ApplicationDbContext>()));
// Configure validation logic for usernames
// *** ADD INT TYPE ARGUMENT TO METHOD CALL:
manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
// Register two factor authentication providers.
// This application uses Phone and Emails as a step of receiving a
// code for verifying the user You can write your own provider and plug in here.
// *** ADD INT TYPE ARGUMENT TO METHOD CALL:
manager.RegisterTwoFactorProvider("PhoneCode",
new PhoneNumberTokenProvider<ApplicationUser, int>
{
MessageFormat = "Your security code is: {0}"
});
// *** ADD INT TYPE ARGUMENT TO METHOD CALL:
manager.RegisterTwoFactorProvider("EmailCode",
new EmailTokenProvider<ApplicationUser, int>
{
Subject = "SecurityCode",
BodyFormat = "Your security code is {0}"
});
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
// *** ADD INT TYPE ARGUMENT TO METHOD CALL:
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser, int>(
dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
}