问题描述
我正在使用代码优先方法.我已经创建了自己的用户模型和成员资格提供程序.我的模型具有以下某些字段:
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; }
}
问题是我的数据库中没有ConfirmPassword
列(出于明显的原因).如何从数据库中隐藏"此内容,但如何允许视图查看其使用情况.将其更改为private
会将其从数据库中隐藏,但是视图不喜欢这样.
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?
推荐答案
使用[NotMapped]
属性标记您不想持久保存到数据库的列.
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.
这篇关于“隐藏"数据库中的列,但不在实体框架中查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!