我将 EF Core 连接到 MySql,并且有一个名为:
帖子浏览量
我读了这个article说我可以使用数据库视图的查询类型。
如果我只调用 _context.PostViews ,它就可以工作,但如果我在它上面使用 Include ,例如:
_context.PostViews.Include(xxxx),它会抛出此错误:
System.InvalidOperationException:'属性'Comment'不是
实体类型“PostWithViews”的导航属性。这
“include(string)”方法只能与“.”一起使用分开的列表
导航属性名称。”
![enter image description here](https://i.stack.imgur.com/Ns1bR.png)
PostView 具有 Post 中的所有属性(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 几乎相同,但多了 1 个属性视图:
public int Views { get; set; }
这是我如何包含属性,例如评论 :
return GetAll()
.Include(p => p.Author)
.Include(p => p.Comment)
目前(EF Core 2.x)查询类型不支持集合导航属性,正如在将查询类型与实体类型进行比较文档主题:
所以虽然你的Comment
属性看起来像集合导航属性,对于 EF Core 来说它不是,因此不能用于Include
/ ThenInclude
(或 LINQ to Entities 查询)。
However Apartment
, Author
and Category
are “引用指向实体的导航属性”,所以它们应该功能齐全。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)