实体框架检查属性是否是导航属性 [英] entity framework check if property is navigation property

查看:85
本文介绍了实体框架检查属性是否是导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法查看实体的属性是否是导航属性?

Is there any way to see if property of an entity is navigation property, from its metadata?

我可以通过检查实现属性来确定属性是否是实体集合ICOLlection,从那里我可以得出结论,如果它是导航属性。

I can determine if property is entity collection by inspecting if it implements ICollection and from there i can conclude if it is navigation property.

但是,如果属性不是实体集合,而只是引用另一个实体呢?

But what about if property is not entity collection but only reference to another entity?

推荐答案

您可以从MetdataWorkspace获取O-Space EDM实体类型,并具有NavigationProperties属性。这是一个例子:

You can get the O-Space EDM entity type from MetdataWorkspace and it has NavigationProperties property. Here is an example:

var workspace = ((IObjectContextAdapter) ctx).ObjectContext.MetadataWorkspace;
var itemCollection = (ObjectItemCollection)(workspace.GetItemCollection(DataSpace.OSpace));
var entityType = itemCollection.OfType<EntityType>().Single(e => itemCollection.GetClrType(e) == typeof(MyEntity));
foreach(var navigationProperty in entityType.NavigationProperties)
{
    Console.WriteLine(navigationProperty.Name);
}

这篇关于实体框架检查属性是否是导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆