ASPNET身份使用GUID作为关键 [英] aspnet identity using guid as key
问题描述
我想使用的GUID的字符串,而不是我的主键和遵循以下职位:<一href=\"http://stackoverflow.com/questions/19553424/how-to-change-type-of-id-in-microsoft-aspnet-identity-entityframework-identityus\">How改变ID的类型Microsoft.AspNet.Identity.EntityFramework.IdentityUser 并
<一href=\"http://stackoverflow.com/questions/19553424/how-to-change-type-of-id-in-microsoft-aspnet-identity-entityframework-identityus/19577113#19577113\">How改变Microsoft.AspNet.Identity.EntityFramework.IdentityUser ID类型
我更新到ASPNET身份的最新$ P $租赁前的包
微软ASP.NET身份核心2.0.0-β1
微软ASP.NET身份的EntityFramework 2.0.0-β1
块引用>和编辑我的用户允许的GUID,而不是默认的字符串,然后,我创建了自己的DbContext和的UserManager,但是我尝试登录我得到以下错误每次:
System.Data.SqlClient.SqlException:操作数类型冲突:
唯一标识符是使用int不兼容
块引用>这条线:
VAR用户=等待UserManager.FindAsync(model.UserName,
model.Password);
块引用>我已经检查,以确保数据库中的所有领域是绝对uniqueidentifiers,我也不一定要尝试下一个是什么,下面是我目前使用code:
用户对象:
公共类GuidRole:IdentityRole&LT; GUID,请GuidUserRole&GT;
{
公共GuidRole()
{
ID = Guid.NewGuid();
}
公共GuidRole(字符串名称):这个(){名称=名称; }
}
公共类GuidUserRole:IdentityUserRole&LT;&的Guid GT; {}
公共类GuidUserClaim:IdentityUserClaim&LT;&的Guid GT; {}
公共类GuidUserLogin:IdentityUserLogin&LT;&的Guid GT; {} 公共类用户:IdentityUser&LT; GUID,请GuidUserLogin,GuidUserRole,GuidUserClaim&GT;
{
公共用户()
{
ID = Guid.NewGuid();
} 公共用户(字符串名称):这个(){用户名=名称; } 公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
}的DbContext:
公共类newDbContext:IdentityDbContext&lt;使用者,GuidRole,GUID GuidUserLogin,GuidUserRole,GuidUserClaim&GT;
{
公共newDbContext()
:基座(nameOrConnectionString:defaultConnection){} 公共newDbContext(字符串的connectionString)
:基座(nameOrConnectionString:的connectionString){} 静态newDbContext()
{
Database.SetInitializer&LT; newDbContext&GT;(NULL);
}
保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
//使用单数的表名
base.OnModelCreating(模型构建器);
modelBuilder.Conventions.Remove&LT; PluralizingTableNameConvention&GT;(); modelBuilder.Entity&lt;使用者&GT;()ToTable(用户),性质(P =&GT; p.Id)。.HasColumnName(用户名);
modelBuilder.Entity&lt;使用者&GT;()房产(P =&GT; p.Email)。.HasColumnName(EmailAddress的); modelBuilder.Entity&LT; GuidUserRole方式&gt;()HasKey(R = gt;新建{r.RoleId,r.UserId});
modelBuilder.Entity&LT; GuidUserRole&GT;()ToTable(的UserRole);
modelBuilder.Entity&LT; GuidUserRole&GT;()房产(R = GT; r.UserId)。.HasColumnName(用户名);
modelBuilder.Entity&LT; GuidUserRole&GT;()房产(R = GT; r.RoleId)。.HasColumnName(角色ID); modelBuilder.Entity&LT; GuidUserLogin&GT;()ToTable(用户登陆);
modelBuilder.Entity&LT; GuidUserLogin&GT;()房产(R = GT; r.UserId)。.HasColumnName(用户名); modelBuilder.Entity&LT; GuidUserClaim&GT;()ToTable(UserClaim);
modelBuilder.Entity&LT; GuidUserClaim&GT;()房产(R = GT; r.Id)。.HasColumnName(UserClaimID); modelBuilder.Entity&LT; GuidRole&GT;()HasKey&LT;&的Guid GT(R = GT; r.Id);
modelBuilder.Entity&LT; GuidRole&GT;()ToTable(角色);
modelBuilder.Entity&LT; GuidRole&GT;()房产(R = GT; r.Id)。.HasColumnName(角色ID); Configuration.ProxyCreationEnabled = FALSE;
Configuration.LazyLoadingEnabled = FALSE;
}}和最终用户管理器:
公共类ApplicationUserManager:的UserManager&lt;使用者中GUID&GT;
{
公共ApplicationUserManager(字符串的connectionString)
:基地(新UserStore&lt;使用者,GuidRole,GUID GuidUserLogin,GuidUserRole,GuidUserClaim&GT;(新newDbContext()))
{ UserValidator =新UserValidator&lt;使用者中GUID&GT;(本){AllowOnlyAlphanumericUserNames = FALSE};
}
}
解决方案感谢郝孔祥熙的意见我个人通过表和属性映射去,直到我到了UserClaims表。原来我有字段类型设置在数据库中的唯一标识符,然而,这仍然需要是一个int。改变它解决了这一问题!
I am trying to use Guid's instead of strings for my primary key and have followed the following posts: How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser and How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser
I updated to the latest prerelease packages of aspnet identity
Microsoft ASP.NET Identity Core 2.0.0-beta1
Microsoft ASP.NET Identity EntityFramework 2.0.0-beta1
and edited my User to allow for Guid's instead of the default string, I then created my own dbContext and usermanager, however every time I try to login I get the following error:
System.Data.SqlClient.SqlException: Operand type clash: uniqueidentifier is incompatible with int
for this line:
var user = await UserManager.FindAsync(model.UserName, model.Password);
I have checked to make sure that all the fields in the database are definitely uniqueidentifiers and I'm not sure what to try next, below is the code I am currently using:
User objects:
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 User : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { public User() { Id = Guid.NewGuid(); } public User(string name) : this() { UserName = name; } public string FirstName { get; set; } public string LastName { get; set; } }
dbContext:
public class newDbContext : IdentityDbContext<User, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim> { public newDbContext() : base(nameOrConnectionString: "defaultConnection") { } public newDbContext(string connectionString) : base(nameOrConnectionString: connectionString) { } static newDbContext() { Database.SetInitializer<newDbContext>(null); } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Use singular table names base.OnModelCreating(modelBuilder); modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Entity<User>().ToTable("User").Property(p => p.Id).HasColumnName("UserID"); modelBuilder.Entity<User>().Property(p => p.Email).HasColumnName("EmailAddress"); modelBuilder.Entity<GuidUserRole>().HasKey(r => new { r.RoleId, r.UserId }); modelBuilder.Entity<GuidUserRole>().ToTable("UserRole"); modelBuilder.Entity<GuidUserRole>().Property(r => r.UserId).HasColumnName("UserID"); modelBuilder.Entity<GuidUserRole>().Property(r => r.RoleId).HasColumnName("RoleID"); modelBuilder.Entity<GuidUserLogin>().ToTable("UserLogin"); modelBuilder.Entity<GuidUserLogin>().Property(r => r.UserId).HasColumnName("UserID"); modelBuilder.Entity<GuidUserClaim>().ToTable("UserClaim"); modelBuilder.Entity<GuidUserClaim>().Property(r => r.Id).HasColumnName("UserClaimID"); modelBuilder.Entity<GuidRole>().HasKey<Guid>(r => r.Id); modelBuilder.Entity<GuidRole>().ToTable("Role"); modelBuilder.Entity<GuidRole>().Property(r => r.Id).HasColumnName("RoleID"); Configuration.ProxyCreationEnabled = false; Configuration.LazyLoadingEnabled = false; } }
and finally the user manager:
public class ApplicationUserManager : UserManager<User, Guid> { public ApplicationUserManager(string connectionString) : base(new UserStore<User, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>(new newDbContext())) { UserValidator = new UserValidator<User, Guid>(this) { AllowOnlyAlphanumericUserNames = false }; } }
解决方案Thanks to Hao Kung's comment I individually went through the table and property mappings until I got to the UserClaims table. Turns out I had the field type set to uniqueidentifier in the database, however this still needed to be an int. Changing it fixed the problem!
这篇关于ASPNET身份使用GUID作为关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!