我已经将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)
发布于 2019-05-16 03:27:57
目前(EF Core2.x)查询类型不支持集合导航属性,如Compare query types to entity types文档主题中所述:
因此,尽管您的Comment
属性看起来像集合导航属性,但对于EF核心,它不是,因此不能在Include
/ ThenInclude
(或LINQ to Entities查询)中使用。
然而,Apartment
、Author
和Category
是“指向实体的引用导航属性”,因此它们应该是全功能的。
发布于 2019-05-15 03:42:02
include函数需要一个查询参数,但它仍然不知道它的对象,因此您需要使用lambda函数来传递它的对象和所需的参数
试试:_context.PostView.Include(lambda => lambda.Comment);
https://stackoverflow.com/questions/56137189
复制相似问题