首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >实体框架核心使用包括在QueryType上(数据库视图)

实体框架核心使用包括在QueryType上(数据库视图)
EN

Stack Overflow用户
提问于 2019-05-15 03:18:15
回答 2查看 1.7K关注 0票数 2

我已经将EF核心连接到MySql,并且我有一个名为:

PostViews

我读到了这篇article,上面说我可以为数据库视图使用查询类型。

如果我只调用_context.PostViews,它可以工作,但是如果我对它使用Include,就像这样:

_context.PostViews.Include(xxxx),它抛出这个错误:

System.InvalidOperationException:‘'Comment’属性不是实体类型'PostWithViews‘的导航属性。“Include(String)”方法只能与“”一起使用。“”导航属性名称的分隔列表。‘

PostView拥有帖子中的所有属性(id,标题,内容,评论等),另外它还有一个额外的列,叫做: Views,它显示了有多少人阅读了这篇文章。

这是我的帖子:

public partial class Post
    {
        public Post()
        {
            Comment = new HashSet<Comment>();
        }

        public string Id { get; set; }
        public string ApartmentId { get; set; }
        public string AuthorId { get; set; }
        public string CategoryId { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime? DueDate { get; set; }
        public bool? Disabled { get; set; }
        public DateTime? CreatedAt { get; set; }
        public DateTime? UpdatedAt { get; set; }
        public string UpdatedBy { get; set; }

        public virtual Apartment Apartment { get; set; }
        public virtual User Author { get; set; }
        public virtual PostCategory Category { get; set; }}
        public virtual ICollection<Comment> Comment { get; set; }
    }

PostWithViews类与Post类几乎相同,但多了一个属性视图:

public int Views { get; set; }

下面是我如何包含属性,例如注释:

return GetAll()
       .Include(p => p.Author)
       .Include(p => p.Comment)
EN

回答 2

Stack Overflow用户

发布于 2019-05-16 03:27:57

目前(EF Core2.x)查询类型不支持集合导航属性,如Compare query types to entity types文档主题中所述:

  • 它们只能包含指向实体的引用导航属性。

因此,尽管您的Comment属性看起来像集合导航属性,但对于EF核心,它不是,因此不能在Include / ThenInclude (或LINQ to Entities查询)中使用。

然而,ApartmentAuthorCategory是“指向实体的引用导航属性”,因此它们应该是全功能的。

票数 3
EN

Stack Overflow用户

发布于 2019-05-15 03:42:02

include函数需要一个查询参数,但它仍然不知道它的对象,因此您需要使用lambda函数来传递它的对象和所需的参数

试试:_context.PostView.Include(lambda => lambda.Comment);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56137189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档