在我的模型中有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();
}
}
}
我收到了无效列名 ConfirmPassword
的错误那怎么解决呢?
答案 0 :(得分:0)
使用 [NotMapped] 属性标记您不想持久保存到数据库的列。
ForeverDebugging
所述的其他方法"Hide" column from database, but not View in Entity Framework
的详细信息