从 .Net Core 1.1 中的 ModelMetaData 获取 RelatedEntities
Get RelatedEntities from ModelMetaData in .Net Core 1.1
我试图在脚手架上下文中获取 .Net Core 中的相关实体。
我正在 MVC 中构建一个正在构建的详细信息页面。我需要属性和导航。正在显示所有相关属性,但是,只有 非 ICollection 导航属性显示在 .Navigations.
中
正在加载的对象是
Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.ModelMetaData
foreach (var property in Model.ModelMetadata.Properties)
{
if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey)
{
<dt>
@@Html.DisplayNameFor(model => model.@GetValueExpression(property))
</dt>
<dd>
@@Html.DisplayFor(model => model.@GetValueExpression(property))
</dd>
}
}
foreach (var navigation in Model.ModelMetadata.Navigations)
{
<dt>
@@Html.DisplayNameFor(model => model.@GetValueExpression(navigation))
</dt>
<dd>
<a asp-area="" @GetNavigationLinkExpression(navigation)>@@Html.DisplayFor(model => model.@GetValueExpression(navigation).@navigation.DisplayPropertyName)</a>
</dd>
}
我的模型是这样的...并且 ModelMetaData 只能浏览第二个并跳过第一个。我在哪里可以访问这里的第一个 属性 以便我可以对其进行模板化?
public virtual ICollection<SomeModel> CollectionNavigationProperty1 { get; set; }
public virtual AnotherSomeModel NavigationProperty1 { get; set; }
好吧,经过几个小时的尝试,我设法找到了一个远没有我希望的那样动态的解决方法。我只是不知道如何获取实体模型的类型对象。
您可以得到如下的ICollection对象列表:
var icollections = new List<string>();
foreach (PropertyInfo property in
typeof(YourProjectName.ContextObject).Assembly
.GetType("YourProjectName.Entities." + Model.ModelTypeName)
.GetProperties())
{
if (property.PropertyType.IsGenericType
&& property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
{
icollections.Add(property.Name);
}
}
将 'YourProjectName.ContextObject' 替换为 DbContext 对象的完整命名空间和类名。
将 'YourProjectName.Entities.' 替换为实体所在位置的完整命名空间。
只有当您的实体/模型保存在同一个命名空间中时,这才会正常工作。
希望有人能提供更好的解决方案,但这将是我现在使用的。
我试图在脚手架上下文中获取 .Net Core 中的相关实体。
我正在 MVC 中构建一个正在构建的详细信息页面。我需要属性和导航。正在显示所有相关属性,但是,只有 非 ICollection 导航属性显示在 .Navigations.
中正在加载的对象是 Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.ModelMetaData
foreach (var property in Model.ModelMetadata.Properties)
{
if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey)
{
<dt>
@@Html.DisplayNameFor(model => model.@GetValueExpression(property))
</dt>
<dd>
@@Html.DisplayFor(model => model.@GetValueExpression(property))
</dd>
}
}
foreach (var navigation in Model.ModelMetadata.Navigations)
{
<dt>
@@Html.DisplayNameFor(model => model.@GetValueExpression(navigation))
</dt>
<dd>
<a asp-area="" @GetNavigationLinkExpression(navigation)>@@Html.DisplayFor(model => model.@GetValueExpression(navigation).@navigation.DisplayPropertyName)</a>
</dd>
}
我的模型是这样的...并且 ModelMetaData 只能浏览第二个并跳过第一个。我在哪里可以访问这里的第一个 属性 以便我可以对其进行模板化?
public virtual ICollection<SomeModel> CollectionNavigationProperty1 { get; set; }
public virtual AnotherSomeModel NavigationProperty1 { get; set; }
好吧,经过几个小时的尝试,我设法找到了一个远没有我希望的那样动态的解决方法。我只是不知道如何获取实体模型的类型对象。
您可以得到如下的ICollection对象列表:
var icollections = new List<string>();
foreach (PropertyInfo property in
typeof(YourProjectName.ContextObject).Assembly
.GetType("YourProjectName.Entities." + Model.ModelTypeName)
.GetProperties())
{
if (property.PropertyType.IsGenericType
&& property.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>))
{
icollections.Add(property.Name);
}
}
将 'YourProjectName.ContextObject' 替换为 DbContext 对象的完整命名空间和类名。 将 'YourProjectName.Entities.' 替换为实体所在位置的完整命名空间。
只有当您的实体/模型保存在同一个命名空间中时,这才会正常工作。
希望有人能提供更好的解决方案,但这将是我现在使用的。