如何使用Entity Framework注册用户模型?

时间:2013-01-20 11:33:12

标签: asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-5

在我的模型中有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

的错误

那怎么解决呢?

1 个答案:

答案 0 :(得分:0)

使用 [NotMapped] 属性标记您不想持久保存到数据库的列。

ForeverDebugging

所述的其他方法

"Hide" column from database, but not View in Entity Framework

的详细信息
相关问题