Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从模型类访问虚拟财产_C# - Fatal编程技术网

C# 如何从模型类访问虚拟财产

C# 如何从模型类访问虚拟财产,c#,C#,在我的项目中,我有低于标准的课程。我需要通过Blog类访问该虚拟财产Posts。请告诉我任何解决办法 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 ICollec

在我的项目中,我有低于标准的课程。我需要通过
Blog
类访问该虚拟财产
Posts
。请告诉我任何解决办法

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; } 
}
公共类博客{
public int BlogId{get;set;}
公共字符串名称{get;set;}
公共字符串Url{get;set;}
公共字符串标记{get;set;}
公共虚拟ICollection Posts{get;set;}
}

我想您希望从Post访问Blog父对象,所以如果我们讨论的是实体框架,您应该在Post模型中创建一个导航属性

[ForeignKey(nameof(Blog))]
public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
EF将从Post创建对Blog父级的访问,您可以使用属性访问Blog

Blog Parent = Post.Blog;

怎么样:
Blog b=GetBlog();b、 帖子
?可能吗?因为它的虚拟属性,我建议您在学习实体框架(这很可能是您的情况)或您在模型中使用的任何库之前先学习该语言。