I am using Identity in .NET Core 6. I would like to have Guid as Id in the User table. So I passed Guid datatype to IdentityUser class in ApplicationUser like this:
public class ApplicationUser:IdentityUser<Guid>
{
public string FullName { get; set; }
}
This is the configuration class of Identity:
public static IServiceCollection AddIdentityService(this IServiceCollection services, IConfiguration configuration)
{
var sqlConnection = configuration["ConnectionStrings:SqlConnection"];
services.AddDbContext<IdentityDataBaseContext>(options => options.UseSqlServer(sqlConnection));
services.AddScoped<IUserService, UserService>();
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<IdentityDataBaseContext>()
.AddDefaultTokenProviders()
.AddRoles<ApplicationRole>()
.AddErrorDescriber<IdentityCustomError>() ;
services.AddScoped<IValidator<CreateUserDto>, CreateUserDtoValidation>();
services.AddScoped<IValidator<LoginUserDto>, LoginUserDtoValidation>();
services.Configure<IdentityOptions>(options =>
{
options.Password.RequiredLength = 8;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = true;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
});
return services;
}
This is the DatabaseContext:
public class IdentityDataBaseContext:IdentityDbContext<ApplicationUser,ApplicationRole,Guid>
{
public IdentityDataBaseContext(DbContextOptions<IdentityDataBaseContext> options):base(options)
{
}
}
When I call FirstOrDefault or First or FirstAsync methods or any other methods for retrieving user data from database, I get this error:
Unable to cast object of type 'System.String' to type 'System.Guid'
This is my AspNetUsers table in SQL Server:
Structure:

Sample data:

Any help would be appreciated. Thanks