如何将AspNetUser
表的PK列从guid
数据类型更改为int
数据类型?今天发布的最新asp.net-identity
版本现在应该可以实现这一点。
但是我找不到这是怎么做的?
. NET Identity(使用实体框架)使用字符串作为主键,而不是guid,但它确实在那些字符串中存储guid。
你需要定义更多的类,我刚刚创建了一个新项目(我使用VS2013 Update 2 CTP),这里是你需要改变的身份模型:
public class ApplicationUser : IdentityUser<int, 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 ApplicationUserRole : IdentityUserRole<int>
{
}
public class ApplicationUserLogin : IdentityUserLogin<int>
{
}
public class ApplicationUserClaim : IdentityUserClaim<int>
{
}
public class ApplicationRole : IdentityRole<int, ApplicationUserRole>
{
}
public class ApplicatonUserStore :
UserStore<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicatonUserStore(ApplicationDbContext context)
: base(context)
{
}
}
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
您还需要更新其他一些地方,只需遵循编译错误,最常见的更改将需要将从User.Identity.GetUserId()返回的字符串转换为整数。
同样,在回答另一个问题(我确实问过这个问题)时,我提供了一个示例解决方案,可以做到这一点,请参阅下面的存储库:
https://github.com/JSkimming/AspNet.Identity.EntityFramework.Multitenant如果有人在寻找身份3.0指南时发现这个:
public class ApplicationUser : IdentityUser<int>
{
}
public class ApplicationRole : IdentityRole<int>
{
}
public class ApplicationDbContext:
IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
}
在Startup.cs文件,ConfigureServices方法:
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext, int>()//, int being the only difference
.AddDefaultTokenProviders();
就是这样,不需要像2.0那样创建管理器
在这篇博文中找到了这个
看看hao-kung在这篇文章中的回答
所以如果你想要int型id,你需要创建你自己的POCO IUser类,并在1.0 RTM版本中为你的自定义IUser类实现你的IUserStore。
这是我们没有时间支持的东西,但我现在正在考虑在1.1中使它更容易(更容易)。希望能尽快在夜间构建中提供一些东西。
更新为1.1-alpha1示例:如何获得每晚构建
如果您更新到最新的夜间比特,您可以尝试新的1.1-alpha1 api,它现在应该使这更容易:下面是插入Guids而不是字符串的样子,例如
public class GuidRole : IdentityRole<Guid, GuidUserRole> {
public GuidRole() {
Id = Guid.NewGuid();
}
public GuidRole(string name) : this() { Name = name; }
}
public class GuidUserRole : IdentityUserRole<Guid> { }
public class GuidUserClaim : IdentityUserClaim<Guid> { }
public class GuidUserLogin : IdentityUserLogin<Guid> { }
public class GuidUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> {
public GuidUser() {
Id = Guid.NewGuid();
}
public GuidUser(string name) : this() { UserName = name; }
}
private class GuidUserContext : IdentityDbContext<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { }
private class GuidUserStore : UserStore<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> {
public GuidUserStore(DbContext context)
: base(context) {
}
}
private class GuidRoleStore : RoleStore<GuidRole, Guid, GuidUserRole> {
public GuidRoleStore(DbContext context)
: base(context) {
}
}
[TestMethod]
public async Task CustomUserGuidKeyTest() {
var manager = new UserManager<GuidUser, Guid>(new GuidUserStore(new GuidUserContext()));
GuidUser[] users = {
new GuidUser() { UserName = "test" },
new GuidUser() { UserName = "test1" },
new GuidUser() { UserName = "test2" },
new GuidUser() { UserName = "test3" }
};
foreach (var user in users) {
UnitTestHelper.IsSuccess(await manager.CreateAsync(user));
}
foreach (var user in users) {
var u = await manager.FindByIdAsync(user.Id);
Assert.IsNotNull(u);
Assert.AreEqual(u.UserName, user.UserName);
}
}