在我的模型中有 4 个字段
[Table("MUser")]
public class UserModel
{
[Required]
[ScaffoldColumn(false)]
[Key, Column("ID", TypeName = "uniqueidentifier")]
public int Id {get; set;}
public string UserName {get; set;}
[Required]
[Column("Password", TypeName = "nvarchar")]
public string Password {get; set;}
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage ="The password and conf password do not match.")]
public string ConfirmPassword {get; set;}
}
注意: ConfirmPassword 不是数据库字段的一部分
所以当尝试保存
[HttpPost]
[AllowAnonymous]
[ActionName("UserRegister")]
public ActionResult Register(UserModel model)
{
if (ModelState.IsValid)
{
using (MyDbContext db = new MyDbContext())
{
db.User.Add(model);
db.SaveChanges();
}
}
}
我收到了 Invalid Column Name ConfirmPassword的错误
那么如何解决呢?