身份将GUID更改为int [英] Identity change GUID to int
问题描述
如何将 AspNetUser
表的PK列从 guid
更改为 int
数据类型?
现在应该可以使用今天发布的最新 asp.net-identity
版本。
How does one change the PK column of the AspNetUser
table from a guid
to int
data type?
This should now be possible with the latest asp.net-identity
version which got released today.
但我无法在任何地方找到这样做的方法?
But I can't find anywhere how this is done?
推荐答案
默认情况下,ASP.NET身份(使用实体框架)使用字符串作为主键,而不是GUID,但它确实将GUID存储在那些字符串中。
By default the ASP.NET Identity (using Entity Framework) uses strings as the primary keys, not GUIDs, but it does store GUIDs in those string.
你需要定义更多的类,我刚创建了一个新的项目(我正在使用VS2013 Update 2 CTP),以下是您需要更改的身份模型:
You need to define a few more classes, I've just created a new project (I'm using the VS2013 Update 2 CTP), here are the identity models you need to change:
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()到一个整数。
You'll also need to update a few other places, just follow the compile errors, the most common change will be the need to convert the string returned form User.Identity.GetUserId() to an integer.
另外,在回答另一个问题时(我虽然确实提出了问题,但我提供了一个示例解决方案,请参阅下面的存储库:
Also, In answering another question (I did ask the question though), I've provided an example solution that does just this, see the repository below:
https://github.com/JSkimming/AspNet.Identity.EntityFramework.Multitenant
这篇关于身份将GUID更改为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!