asp.net-mvc – UserManager.Create(用户,密码)thowing EntityValidationError说Id是必需的?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – UserManager.Create(用户,密码)thowing EntityValidationError说Id是必需的?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有人知道为什么UserManager.Create(用户,密码);可能会抛出EntityValidationError,表示Id是必需的.

它可能与我的UserManager有关.设置如下:

public class MyAppDb : IdentityDbContext<ApplicationUser,ApplicationRole,string,ApplicationUserLogin,ApplicationUserRole,ApplicationUserClaim>
{
    public static MyAppDb Create()
    {
        return new MyAppDb();
    }

    public MyAppDb()
        : base("MyAppDb")
    {
    }
}


public class ApplicationUserStore : UserStore<ApplicationUser,ApplicationUserClaim>
{
    public ApplicationUserStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationUserManager : UserManager<ApplicationUser,string>
{
    public ApplicationUserManager(IUserStore<ApplicationUser,string> store)
        : base(store)
    {
    }
}

在这样的控制器中:

public class AccountController : Controller
{
    public AccountController()
        : this(new ApplicationUserManager(new ApplicationUserStore(new MyAppDb())))
    {
    }

    public AccountController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
        UserManager.PasswordValidator = (IIdentityValidator<string>)new MinimumLengthValidator(8);
        UserManager.UserValidator = new UserValidator<ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false,RequireUniqueEmail = true };
    }

    public ApplicationUserManager UserManager { get; private set; }

    ...
}

ApplicationUser,ApplicationUserRole和ApplicationUserClaim都分别从Identity类继承.

有人看错了吗?在创建没有Id问题的所有自定义Identity类之前,我能够注册用户(UserManager.Create()).

更新:似乎指定Key的字符串已破坏数据库上下文中自动生成的Key.这是我最好的猜测.它现在正在寻找我设置Id.我不想,我希望它在我在用户上创建自定义字段之前的工作方式.

解决方法

看来指定字符串告诉UserStore您将提供Id.当我解决了以下问题 Is there a way to create a custom User and Role without specifying the TKey on IdenitityUser,IdentityRole,and IdentityDbContext?时,这个问题就消失了

-要么-

只需给它一个像这样的ID:Guid.NewGuid().ToString()

原文链接:https://www.f2er.com/aspnet/251817.html

猜你在找的asp.Net相关文章