我已经将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)
https://stackoverflow.com/questions/56137189
复制相似问题