gpt4 book ai didi

asp.net-mvc - 使用 guid 作为 key 的 aspnet 身份

转载 作者:行者123 更新时间:2023-12-04 00:26:15 24 4
gpt4 key购买 nike

我正在尝试使用 Guid 而不是字符串作为我的主键,并遵循以下帖子:How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser
How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUser

我更新到最新的aspnet身份预发布包

Microsoft ASP.NET Identity Core 2.0.0-beta1

Microsoft ASP.NET Identity EntityFramework 2.0.0-beta1



并编辑了我的用户以允许使用 Guid 而不是默认字符串,然后我创建了自己的 dbContext 和用户管理器,但是每次尝试登录时都会出现以下错误:

System.Data.SqlClient.SqlException: Operand type clash: uniqueidentifier is incompatible with int



对于这一行:

var user = await UserManager.FindAsync(model.UserName, model.Password);



我已经检查以确保数据库中的所有字段都是唯一标识符,但我不确定接下来要尝试什么,下面是我目前使用的代码:

用户对象:
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; }
}

数据库上下文:
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;
}

}

最后是用户管理器:
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 };
}
}

最佳答案

感谢 Hao Kung 的评论,我单独浏览了表和属性映射,直到到达 UserClaims 表。原来我在数据库中将字段类型设置为 uniqueidentifier ,但这仍然需要是一个 int 。改变它解决了问题!

关于asp.net-mvc - 使用 guid 作为 key 的 aspnet 身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22504951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com