grandchamp / Identity.Dapper

Identity package that uses Dapper instead EntityFramework for use with .NET Core
MIT License
268 stars 62 forks source link

using guid response a cast error #77

Open eld1887 opened 6 years ago

eld1887 commented 6 years ago

hi, i get the error:

Invalid cast from 'System.UInt64' to 'System.Guid'. System.InvalidCastException: Invalid cast from 'System.UInt64' to 'System.Guid'. at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) at Dapper.SqlMapper.Parse[T](Object value) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2969 at Dapper.SqlMapper.<ExecuteScalarImplAsync>d__641.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.Async.cs:line 1193`

this is my ConfigureServices:

public void ConfigureServices(IServiceCollection services)
        {

            services.ConfigureDapperConnectionProvider<MySqlConnectionProvider>(Configuration.GetSection("DapperIdentity"))
                    .ConfigureDapperIdentityCryptography(Configuration.GetSection("DapperIdentityCryptography"))
                    .ConfigureDapperIdentityOptions(new DapperIdentityOptions { UseTransactionalBehavior = false }); //Change to True to use Transactions in all operations

            services.AddIdentity<CustomUser, CustomRole>(x =>
                                                         {
                                                             x.Password.RequireDigit = false;
                                                             x.Password.RequiredLength = 1;
                                                             x.Password.RequireLowercase = false;
                                                             x.Password.RequireNonAlphanumeric = false;
                                                             x.Password.RequireUppercase = false;
                                                         })

                .AddDapperIdentityFor<MySqlConfiguration, Guid>()
                    .AddDefaultTokenProviders();

            services.AddMvc();

CustomUser and CustomRole:

public class CustomUser : DapperIdentityUser<Guid>
    {
        public string FirstName { get; set; }

        public CustomUser() { }
        public CustomUser(string userName) : base(userName) { }
    }

    public class CustomRole : DapperIdentityRole<Guid>
    {

        public CustomRole() { }
        public CustomRole(string roleName) : base(roleName)
        {
        }
    }

I changed the table for identityUser (MySql) like so:

CREATE TABLE IF NOT EXISTS `identityuser` (
  `Id` CHAR(36) NOT NULL,
  `Username` varchar(256) NOT NULL,
  `Email` varchar(256) DEFAULT NULL,
  `FirstName` varchar(256) DEFAULT NULL,
  `EmailConfirmed` bit(1) NOT NULL,
  `PasswordHash` longtext,
  `SecurityStamp` varchar(38) DEFAULT NULL,
  `PhoneNumber` varchar(50) DEFAULT NULL,
  `PhoneNumberConfirmed` bit(1) NOT NULL,
  `TwoFactorEnabled` bit(1) NOT NULL,
  `LockoutEnd` datetime DEFAULT NULL,
  `LockoutEnabled` bit(1) NOT NULL,
  `AccessFailedCount` int(11) NOT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

Is that a mistake from my side or is that a general bug?

EDIT if i register someone the data is insert in the database, but i get the error message from above

grandchamp commented 6 years ago

That is a bit weird, since Dapper itself is trying to convert UInt64 to GUID. I'll investigate.

rippo commented 6 years ago

I believe this is a mysql connector issue and not an issue in Dapper. I tend to use id BINARY(16) NOT NULL,

grandchamp commented 6 years ago

Did you guys try to update MySQL Connector version?