EF代码优先的NotMapped属性 [英] EF Code first NotMapped Attribute
问题描述
在下面的示例中,为什么需要[NotMapped]属性:
公共虚拟ICollection< Blog>博客{get;组; }
[未映射]
公共列表< Blog> NewBlogs {
get {
return Blogs.Where(x => x.Date> DateTime.Now).ToList();
}
}
如果没有[NotMapped]属性,则会出现异常:
无效的列名Blog_ID
数据库中的列名是BlogID。
EDIT
我希望,没有设置器的属性永远不会直接映射到数据库,并且不会被代码首先自动忽略。
具有[NotMapped]属性的基本上是将属性标记为不是实体/属性
,因此EF不会尝试从数据库映射/获取该属性
<该示例实际上说,NewBlogs不是像Blogs这样的实体。所以停止尝试从数据库中获取NewBlogs
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
这篇关于EF代码优先的NotMapped属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!