Why is in the following example the [NotMapped] attribute required:
public virtual ICollection<Blog> Blogs { get; set; }
[NotMapped]
public List<Blog> NewBlogs{
get{
return Blogs.Where(x=>x.Date > DateTime.Now).ToList();
}
}
Without the [NotMapped] attribute I get an exception:
Invalid column name Blog_ID
The column name in the database is BlogID.
EDIT
I would expect, that properties without setter are never directly mapped to the database and automatically ignored by code first.
with [NotMapped] attribute basically you mark that properties as not an Entity/Properties
so EF will not try to map/fetch that properties from database
that example actually say, NewBlogs is not Entity like Blogs. so stop try to get NewBlogs from database
它被标记为NotMapped,因为它返回按需从DB获取的数据,而不是表示要存储的单独实体集。
Even if the property doesn't have a setter you could have that persisted in the database. ie: you have a model Product that sets a property like the Price in the constructor that you don't want to change (doesn't have a setter) but you want that value persisted in the DB
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.