Mike Wills
Mike Wills

Reputation: 21265

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

I am using a code-first methodology. I have created my own user model and membership provider. My model has some of the following fields:

[Table("mytable")]
public class MyUser
{
    [Key]
    public int UserId { get; set; } // Auto generated

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    public string ConfirmPassword { get; set; }

    [Display(Name = "Your name/company name")] 
    public string Name { get; set; }
}

The problem is that I don't have a ConfirmPassword column in my database (for obvious reasons). How do I "hide" this from the database, but allow the view to be able to see an use it. Changing it to private hides it from the database, but the view doesn't like that.

How can I tell Entity Framework to ignore this field?

Upvotes: 2

Views: 4138

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364319

Mark the column you don't want to persist to database with [NotMapped] attribute.

Btw. this is typical example where you should think about differing between persisted entity and view model for your page = you should think about having two different classes.

Upvotes: 7

Related Questions