相关疑难解决方法(0)

UserManager.Create(用户,密码)thowing EntityValidationError说Id是必需的?

任何人都知道为什么UserManager.Create(user, password);可能会抛出EntityValidationErrorId是必需的.

它可能与我有关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, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUserStore(MyAppDb context)
        : base(context)
    {
    }
}

public class ApplicationUserManager : UserManager<ApplicationUser, string>
{
    public ApplicationUserManager(IUserStore<ApplicationUser, string> store)
        : base(store)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

在这样的控制器中:

public class AccountController : Controller
{ …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc entity-framework entity-framework-6 asp.net-mvc-5 asp.net-identity

5
推荐指数
1
解决办法
6056
查看次数

扩展Roles表后,实体类型IdentityRole不是当前上下文的模型的一部分

我将由实体框架创建的AspNetRoles扩展为如下所示:

public class AspNetRoles:IdentityRole
{
        public AspNetRoles() : base() { }
        public String Label { get; set; }
        public String ApplicationId { get; set; }
        public AspNetApplications Application { get; set; }

        public static readonly String SystemAdministrator = "SystemAdministrator";
}
Run Code Online (Sandbox Code Playgroud)

我知道,因为我已经扩展了identityrole表,所以必须对usermanager进行更改。这是我所做的:

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

    public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    {
        var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
        // Configure validation logic for usernames
        manager.UserValidator = new UserValidator<ApplicationUser>(manager)
        {
            AllowOnlyAlphanumericUserNames …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc entity-framework

4
推荐指数
1
解决办法
1073
查看次数