“Hide” column from database, but not View in Entity Framework

眉间皱痕 提交于 2020-01-04 10:39:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9723340/hide-column-from-database-but-not-view-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!