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()

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...