Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF6代码首次迁移种子方法无法创建用户_C#_Asp.net_Asp.net Mvc 5_Asp.net Identity 2 - Fatal编程技术网

C# EF6代码首次迁移种子方法无法创建用户

C# EF6代码首次迁移种子方法无法创建用户,c#,asp.net,asp.net-mvc-5,asp.net-identity-2,C#,Asp.net,Asp.net Mvc 5,Asp.net Identity 2,我用自定义类实现了Identity 2.1,使所有实体都具有整数键 我在迁移文件夹中的configuration.cs文件中有以下seed方法 protected override void Seed(ApplicationDbContext context) { var roleManager = new ApplicationRoleManager(new ApplicationRoleStore(context)); var userManager = new Applic

我用自定义类实现了Identity 2.1,使所有实体都具有整数键

我在迁移文件夹中的configuration.cs文件中有以下seed方法

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");
        }
    }
}
运行更新数据库方法时,我遇到以下错误:

System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时出错。有关详细信息,请参见内部异常。-->MySql.Data.MySqlClient.MySqlException:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,了解在第33行的SELECT CASE中使用near的正确语法

我的用户商店:

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


    public ApplicationUserStore(DbContext context)
        : base(context)
    {
    }
}
和我的用户经理:

 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;
    }
}

您收到一个SQL错误的事实表明,1您没有正确实现对int-id的更改,2您在实现此更改之前已有一个数据库,并且您在尝试保存/读取int和字符串时遇到冲突。首先,我会炸掉您的开发数据库,让实体框架根据您当前的代码重新创建它。我每次都会删除数据库,以确保不会发生这种错误。数据库创建ok with ints for id,写入角色,并在用户上失败