[HttpPost]
public ActionResult Registration(UserModel user)
{
Console.WriteLine("ja");
try
{
if (ModelState.IsValid)
{
var crypto = new SimpleCrypto.PBKDF2();
var encrpPass = crypto.Compute(user.Password);
UserModel newUser = new UserModel(user.Email, encrpPass);
newUser.PasswordSalt = crypto.Salt;
userRepository.Add(newUser);
userRepository.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
catch (System.Data.Entity.Validation.DbEntityValidationException ex)
{
Console.WriteLine(ex);
}
return View(user);
}
UserModel类:
public class UserModel
{
public int UserModelId { get; set; }
[Required]
[EmailAddress]
[StringLength(150)]
[Display(Name="Email address: ")]
public String Email { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(20, MinimumLength = 6)]
[Display(Name = "Password: ")]
public String Password { get; set; }
public String PasswordSalt { get; set; }
public UserModel(String email, String password)
{
this.Email = email;
this.Password = password;
}
public UserModel()
{
}
}
关于异常的更多详细信息:
消息 "无法对处于添加状态的实体使用OriginalValues。" 字符串
堆栈跟踪:
StackTrace " 在 System.Data.Entity.Internal.InternalContext.SaveChanges()\r\n 在 System.Data.Entity.Internal.LazyInternalContext.SaveChanges()\r\n
在 System.Data.Entity.DbContext.SaveChanges()\r\n 在 SoccerManager1.Models.DAL.UserRepository.SaveChanges() 中 d:\Stijn\Documenten\Visual Studio 2013\Projects\SoccerManager1\SoccerManager1\Models\DAL\UserRepository.cs:regel 48\r\n 在 SoccerManager1.Controllers.UserController.Registration(UserModel user) 中 d:\Stijn\Documenten\Visual Studio 2013\Projects\SoccerManager1\SoccerManager1\Controllers\UserController.cs:regel 72" 字符串
我只是想创建一个注册页面,我不明白为什么会收到这个错误。
我在这里做错了什么?如果我提供的信息不足,请告诉我。