In My Model there is 4 field
[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;}
}
Note: ConfirmPassword is not part of the database field
So when try to save
[HttpPost]
[AllowAnonymous]
[ActionName("UserRegister")]
public ActionResult Register(UserModel model)
{
if (ModelState.IsValid)
{
using (MyDbContext db = new MyDbContext())
{
db.User.Add(model);
db.SaveChanges();
}
}
}
I got the error that Invalid Column Name ConfirmPassword
So how to solve that ?
Mark the column you don’t want to persist to database with [NotMapped] attribute.
Other approach as described by ForeverDebugging
More detail from "Hide" column from database, but not View in Entity Framework