【发布时间】:2014-08-07 23:27:26
【问题描述】:
我刚刚将我的 MVC 5 应用程序(以前是带有 SimpleMembership 的 MVC 3)升级到 ASP.NET Identity 2.0,我可以与现有用户一起工作,但是当我执行以下操作来创建新用户时:
var user = new ApplicationUser();
//user.Id = db.Users.Max(u => u.Id) + 1; //does not help
user.UserName = model.UserName;
user.Email = model.EMailAddress;
var result = await UserManager.CreateAsync(user, model.Password);
我得到以下信息:
System.Data.Entity.Core.UpdateException:更新条目时出错。有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException:无法将值 NULL 插入到列“Id”、表“MyDB.dbo.AspNetUsers”中;列不允许空值。插入失败。 声明已终止。
有人知道为什么会这样吗?
这是我的UserManager:
var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<MyDbContext>()));
manager.UserValidator = new UserValidator<ApplicationUser, int>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = false,
RequireLowercase = false,
RequireUppercase = false,
};
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser, int>(dataProtectionProvider.Create("ASP.NET Identity"));
}
我为用户和角色使用int 键,但我之前有默认的string-键,Id 也没有填充,我得到了类似的错误。
【问题讨论】:
-
你的
ApplicationUser类是什么样的? -
@DavidG:
public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>- 所有Application*-classes 都有int-key 和一些自定义属性。
标签: c# asp.net-mvc entity-framework asp.net-identity-2