使用guid作为密钥的aspnet标识

时间:2014-03-19 11:51:24

标签: asp.net-mvc entity-framework asp.net-identity

我正在尝试使用Guid而不是字符串作为我的主键,并遵循以下帖子:How to change type of id in Microsoft.AspNet.Identity.EntityFramework.IdentityUserHow 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和usermanager,但每次我尝试登录时都会出现以下错误:

  

System.Data.SqlClient.SqlException:操作数类型冲突:   uniqueidentifier与int

不兼容

这一行:

  

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

我已经检查过以确保数据库中的所有字段都是uniqueidentifier,我不知道接下来要尝试什么,下面是我目前使用的代码:

用户对象:

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;
    }

}

最后是用户经理:

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 };
    }
}

1 个答案:

答案 0 :(得分:0)

感谢Hao Kung的评论,我逐个完成了表和属性映射,直到我进入UserClaims表。结果我在数据库中将字段类型设置为uniqueidentifier,但是这仍然需要是int。改变它解决了问题!