0

I have implemented Identity 2.1 with custom classes to make all of the entities to have integer keys.

I have the following seed method in my configuration.cs file in the migrations folder

protected override void Seed(ApplicationDbContext context)
{
    var roleManager = new ApplicationRoleManager(new ApplicationRoleStore(context));
    var userManager = new ApplicationUserManager(new ApplicationUserStore(context));

    if (!roleManager.RoleExists("User"))
    {
        roleManager.Create(new ApplicationRole("User"));
    }
    if (!roleManager.RoleExists("Supervisor"))
    {
        roleManager.Create(new ApplicationRole("Supervisor"));
    }
    if (!roleManager.RoleExists("Administrator"))
    {
        roleManager.Create(new ApplicationRole("Administrator"));
    }
    if (!roleManager.RoleExists("Owner"))
    {
        roleManager.Create(new ApplicationRole("Owner"));
    }
    if (!roleManager.RoleExists("System User"))
    {
        roleManager.Create(new ApplicationRole("System User"));
    }

    if (userManager.FindByName("h2euser") == null)
    {
        var h2Euser = new ApplicationUser
        {
            UserName = "h2euser",
            Email = "test"
        };
        var result = userManager.Create(h2Euser, "Letme1n");

        if (result.Succeeded)
        {
            userManager.AddToRole(h2Euser.Id, "System User");
        }
    }

    if (userManager.FindByName("owner") == null)
    {
        var ownerUser = new ApplicationUser
        {
            UserName = "owner",
            Email = "test2"
        };

        var result = userManager.Create(ownerUser, "ChangeMe!");

        if (result.Succeeded)
        {
            userManager.AddToRole(ownerUser.Id, "Owner");
        }
    }
}

When running the Update-Database method I get this error:

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM ((SELECT CASE WHEN (`Extent2`.`UserId` IS NULL) THEN (NULL) ELSE (1) END' at line 33

My UserStore:

public class ApplicationUserStore
 : UserStore<ApplicationUser, ApplicationRole, int,
     ApplicationUserLogin, ApplicationUserRole,
     ApplicationUserClaim>
{
    public ApplicationUserStore()
        : this(new IdentityDbContext())
    {
        base.DisposeContext = true;
    }


    public ApplicationUserStore(DbContext context)
        : base(context)
    {
    }
}

And my user manager:

 public class ApplicationUserManager : UserManager<ApplicationUser, int>
{
    // *** ADD INT TYPE ARGUMENT TO CONSTRUCTOR CALL:
    public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
        : base(store)
    {
    }

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        // *** PASS CUSTOM APPLICATION USER STORE AS CONSTRUCTOR ARGUMENT:
        var manager = new ApplicationUserManager(
        new ApplicationUserStore(context.Get<ApplicationDbContext>()));

        // Configure validation logic for usernames

        // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
        manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        // Configure validation logic for passwords
        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        // Configure user lockout defaults
        manager.UserLockoutEnabledByDefault = true;
        manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
        manager.MaxFailedAccessAttemptsBeforeLockout = 5;

        // Register two factor authentication providers. 

        // This application uses Phone and Emails as a step of receiving a 
        // code for verifying the user You can write your own provider and plug in here.

        // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
        manager.RegisterTwoFactorProvider("PhoneCode",
            new PhoneNumberTokenProvider<ApplicationUser, int>
            {
                MessageFormat = "Your security code is: {0}"
            });

        // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
        manager.RegisterTwoFactorProvider("EmailCode",
            new EmailTokenProvider<ApplicationUser, int>
            {
                Subject = "SecurityCode",
                BodyFormat = "Your security code is {0}"
            });

        var dataProtectionProvider = options.DataProtectionProvider;

        if (dataProtectionProvider != null)
        {
            // *** ADD INT TYPE ARGUMENT TO METHOD CALL:
            manager.UserTokenProvider =
                new DataProtectorTokenProvider<ApplicationUser, int>(
                    dataProtectionProvider.Create("ASP.NET Identity"));
        }
        return manager;
    }
}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
HarryParker
  • 73
  • 1
  • 6
  • The fact that you're getting a SQL error indicates that either 1) you haven't implemented the change to int ids correctly or 2) you had an existing database before you implemented this change, and you're getting conflicts trying to save/read between ints and strings. I'd start by blowing out your development database and letting Entity Framework recreate it based on your current code. – Chris Pratt Feb 02 '15 at 18:25
  • I'm dropping the database each time to ensure that sort of error isn't happening. The database creates ok with ints for id's, writes the roles, and fails on the users – HarryParker Feb 02 '15 at 19:08

0 Answers0