C# 实体框架:通过FK属性查找相关导航属性

C# 实体框架:通过FK属性查找相关导航属性,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我需要找到FK属性的相关导航属性 这是我的模型: 公共类博客 { public int BlogId{get;set;} 公共字符串Url{get;set;} 公共虚拟ICollection Posts{get;set;} } 公营职位 { 公共int PostId{get;set;} 公共字符串标题{get;set;} 公共字符串体{get;set;} public int BlogId{get;set;} 公共虚拟博客{get;set;} } 公共类BloggingContext:DbCon

我需要找到FK属性的相关导航属性

这是我的模型:

公共类博客
{
public int BlogId{get;set;}
公共字符串Url{get;set;}
公共虚拟ICollection Posts{get;set;}
}
公营职位
{
公共int PostId{get;set;}
公共字符串标题{get;set;}
公共字符串体{get;set;}
public int BlogId{get;set;}
公共虚拟博客{get;set;}
}
公共类BloggingContext:DbContext
{
公共数据库集博客{get;set;}
公共DbSet Posts{get;set;}
}
现在,我想写这样的东西:

我首先使用EF6.2和代码


谢谢你的帮助

经过大量研究,我没有找到ant资源,也没有找到如何做的示例,但我找到了一个临时解决方案,使用以下代码,我不知道这是最佳方法还是有例外

公共静态字符串GetNavigationByForeignKey(此DbContext上下文,类型entityType,字符串fkPropertyName)
{
var objectContext=(上下文为IObjectContextAdapter);
var entityMetadata=objectContext.MetadataWorkspace
.GetItems(DataSpace.CSpace)
.FirstOrDefault(et=>et.Name==entityType.Name);
var navigationProperty=entityMetadata.NavigationProperties
.FirstOrDefault(prop=>prop.GetDependentProperties().Any(dep=>dep.Name==fkPropertyName));
返回navigationProperty.Name;
}
如果有人有更好的解决方案,我将不胜感激

var context = new BloggingContext();
var navigationProperty = GetNavigationProperty(context, typeof(Post), "BlogId"); // returns "Blog"