实体框架核心使用QueryType(数据库视图)上的包含 [英] Entity Framework Core Use Include on QueryType(Database View)

查看:83
本文介绍了实体框架核心使用QueryType(数据库视图)上的包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


PostViews


blockquote>

我阅读了此



PostView在Post中具有所有属性(ID,标题,内容,评论等)。 ,另外还有一列称为:查看显示了这篇文章的人数的视图。






这是我的帖子:

 公共部分类Post 
{
public Post()
{
Comment =新的HashSet< Comment>();
}

公共字符串ID {get;组; }
公用字串ApartmentId {get;组; }
公用字串AuthorId {get;组; }
公共字符串CategoryId {get;组; }
公用字串Title {get;组; }
公共字符串说明{get;组; }
公共DateTime? DueDate {获取;组; }
公共布尔?残疾人{组; }
公共DateTime? CreatedAt {get;组; }
公共DateTime? UpdatedAt {get;组; }
公共字符串UpdatedBy {组; }

公共虚拟公寓公寓{组; }
公共虚拟用户作者{get;组; }
公共虚拟PostCategory类别{get;组; }}
公共虚拟ICollection< Comment>评论{get;组; }
}

PostWithViews类与Post几乎相同,但具有1个以上的属性Views:

  public int Views {get;组; } 

以下是我如何添加属性的信息:注释:

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


解决方案

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



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


因此,尽管您的 Comment 属性看起来像集合导航属性,但对于EF Core而言,它不是,因此不能在<$ c中使用$ c>包含 / 然后包含(或LINQ to Entities查询)。



但是公寓作者类别 r指向实体的参考导航属性 ,因此它们应具有完整的功能。


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.

这篇关于实体框架核心使用QueryType(数据库视图)上的包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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