EF代码优先的NotMapped属性 [英] EF Code first NotMapped Attribute

查看:360
本文介绍了EF代码优先的NotMapped属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,为什么需要[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆