I am trying to use Guid instead of strings for my primary key and have been following the following messages: How to change the identifier type in Microsoft.AspNet.Identity.EntityFramework.IdentityUser and How to change the identifier type in Microsoft.AspNet.Identity.EntityFramework.IdentityUser
I updated to the latest aspnet id prerelease packages
Microsoft ASP.NET Identity Core 2.0.0-beta1
Microsoft ASP.NET Identity EntityFramework 2.0.0-beta1
and edited my user to allow Guid instead of the default line, I then created my own dbContext and usermanager, however every time I try to log in, I get the following error:
System.Data.SqlClient.SqlException: operand: uniqueidentifier type column is not compatible with int
for this line:
var user = wait UserManager.FindAsync (model.UserName, model.Password);
I have verified that all the fields in the database are definitely unique identifiers, and I'm not sure what to do next, below is the code I'm 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 }; } }