4
votes

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 };
    }
}
1
Maybe try without your OnModelCreating as a test, to see if something in there is causing the error?Hao Kung

1 Answers

0
votes

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!