Entity Framework Core 使用 Include on QueryType(Database View) [英] Entity Framework Core Use Include on QueryType(Database View)

查看:15
本文介绍了Entity Framework Core 使用 Include on QueryType(Database View)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 EF Core 连接到 MySql 并且我有一个名为的视图:

<块引用>

帖子视图

我读了这篇

PostView 拥有 Post 中的所有属性(id、标题、内容、评论等),另外还有一个名为:Views 的列,显示有多少人阅读了这篇文章.

<小时>

这是我的帖子:

公共部分类 Post{公共邮政(){Comment = new HashSet();}公共字符串 ID { 获取;放;}公共字符串 ApartmentId { 获取;放;}公共字符串 AuthorId { 获取;放;}公共字符串 CategoryId { 获取;放;}公共字符串标题{获取;放;}公共字符串 描述 { 获取;放;}公共日期时间?截止日期{得到;放;}公共布尔?禁用 { 获取;放;}公共日期时间?CreatedAt { 获取;放;}公共日期时间?更新在 { 获取;放;}公共字符串 UpdatedBy { 获取;放;}公共虚拟公寓公寓{get;放;}公共虚拟用户作者{ get;放;}公共虚拟 PostCategory Category { get;放;}}公共虚拟 ICollection评论 { 得到;放;}}

PostWithViews 类与 Post 几乎相同,但多了 1 个属性 Views:

public int Views { get;放;}

这是我如何包含属性,例如评论:

返回 GetAll().Include(p => p.Author).Include(p => p.Comment)

解决方案

当前 (EF Core 2.x) 查询类型不支持集合导航属性,如比较查询类型与实体类型 文档主题:

<块引用>
  • 它们只能包含指向实体的参考导航属性.

因此,尽管您的 Comment 属性看起来像集合导航属性,但对于 EF Core 而言并非如此,因此不能用于 Include/ThenInclude(或 LINQ to Entities 查询).

但是ApartmentAuthorCategory指向实体的参考导航属性",所以它们应该功能齐全.

I have EF Core connected to MySql and I have a View called:

PostViews

I read this article saying I can use Query Types for Database Views.

It works if I just call _context.PostViews, but if I use Include on it like:

_context.PostViews.Include(xxxx), it throws me this error:

System.InvalidOperationException: 'The property 'Comment' is not a navigation property of entity type 'PostWithViews'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.'

PostView has all properties in Post (id, title, content, Comment etc.), plus it has one extra column called: Views which shows how many people have read this post.


Here is my Post:

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 class is almost the same with Post but with 1 more property Views:

public int Views { get; set; }

Here is how I include properties e.g. Comment :

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

解决方案

Currently (EF Core 2.x) query types do not support collection navigation properties, as mentioned in the Compare query types to entity types documentation topic:

  • They can only contain reference navigation properties pointing to entities.

So although your Comment property looks like collection navigation property, for EF Core it s isn't, hence cannot be used in Include / ThenInclude (or LINQ to Entities query).

However Apartment, Author and Category are "reference navigation properties pointing to entities", so they should be fully functional.

这篇关于Entity Framework Core 使用 Include on QueryType(Database View)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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