如何从模型类访问虚拟属性

如何从模型类访问虚拟属性

问题描述:

我的项目中有以下模型类。我需要从该虚拟资产PostsBlog类访问。请让我知道任何解决方案如何从模型类访问虚拟属性

public class Blog { 
    public int BlogId { get; set; } 
    public string Name { get; set; } 
    public string Url { get; set; } 
    public string Tags { get; set; } 
    public virtual ICollection<Post> Posts { get; set; } 
} 
+1

怎么样:'Blog b = GetBlog(); b.Posts'? – Bas

+0

有可能吗?因为它的虚拟属性 –

+0

我建议你在学习实体框架(这很可能是你的情况)之前学习语言,或者你在模型中使用的任何库。 – grek40

我想你想从帖子访问博客家长,所以如果我们谈论的是实体框架,你应该建立在邮政型号导航属性。

[ForeignKey(nameof(Blog))] 
public int BlogId { get; set; } 
public virtual Blog Blog { get; set; } 

EF将创建从博客的博客家长访问,并且您可以使用该属性访问博客。

Blog Parent = Post.Blog;