问题描述
在运行时,我想查找一个EntityObject是否还有一个给定的NavigationProperty的外键属性。我下面有两个步骤。我想这将需要一些元数据查询。
At run time I would like to find whether an EntityObject also has a foreign key property for a given NavigationProperty. I have this in two steps below. I imagine this is going to require some metadata querying.
我不确定如何测试元数据类是否指向特定EntityObject类型的类型:即我在概念上知道但不是程式上的EntityType实例和EntityObject实例之间的关系。
I am unsure how to test whether a metadata class points to the type of a particular EntityObject type: ie I know conceptually but not programmically the relationship between an EntityType instance and an EntityObject instance.
到目前为止,我有:
/*puesdo code class representing edm example*/
class Possesion: EntityObject
{
//Nav prop
public Person Owner { get; set; }
//related FK prop
public int OwnerId { get; set; }
}
public static NavigationProperty GetNavigationProperty<TObjectContext, T>(
this ObjectContext context,
Expression<Func<T, Object>> targetProperty)
where TObjectContext : ObjectContext
{
//eg: possession => possession.Owner property type would be person
PropertyInfo targetProp = GetPropertyType(targetProperty);
//target type would be Possesion
Type targetType = targetProp.DeclaringType;
var containerName = context.DefaultContainerName;
var model = DataSpace.CSpace;
var workspace = context.MetadataWorkspace;
var container = workspace.GetEntityContainer(containerName, model);
EntitySetBase entitySet = container.BaseEntitySets
.Where(e => e.Name == context.GetEntitySetNameFromType(targetType))
.FirstOrDefault();
if (entitySet == null)
return null;
//materialize nav props for testing
var navigationProps = entitySet.ElementType.Members
.Where(m => m.BuiltInTypeKind == BuiltInTypeKind.NavigationProperty
)
.Cast<NavigationProperty>()
.ToList();
//The question: how to filter the nav props for that which pertains
// to the target property?
NavigationProperty navProp = navigationProps
.Where(
//how do we select the nav property based on the Passed EntityObject's type
//ie how to we link the Metadata type to the concrete type??
n => n.FromEndMember.TypeUsage.???????? == targetProp.PropertyType)
.FirstOrDefault();
return navProp;
}
public static String GetPossibleFKPropertyName(Type entityObjectType, NavigationProperty property)
{
//Check each end for the target type
//with this end ie To or From, determine if there is a Property on the entityObject that equated to the PropertyRef column
//return this.
var toEnd = property.ToEndMember.GetEntityType();
//Again how do I determine that the Person metadata is a Person EntityObjectType?
if (toEnd.SomeTypeMember???? == entityObjectType)
return toEnd.KeyMembers[0].Name; //Testing HACK, return name for now.
var fromEnd = property.FromEndMember.GetEntityType();
if (fromEnd.SomeTypeMember???? == entityObjectType)
return fromEnd.KeyMembers[0].Name;
return "";
}
推荐答案
简单地对ElementType的FullName进行文本比较
I ended up simply doing a text comparison on the FullName of the ElementType
ie:
n =>((RefType)n.ToEndMember.TypeUsage.EdmType).ElementType.FullName
== targetProp.PropertyType.FullName)
还有ToEnd是我定位的属性,一旦我有NavigationProperty,调用 GetDependentProperties()
似乎产生FK属性名称之后我是:
also the ToEnd being the property I am targeting then once I had the NavigationProperty, calling GetDependentProperties()
seems to yield what I am after being the FK property name ie:
public static String GetFKPropertyName(this NavigationProperty property)
{
var depProp = property.GetDependentProperties().FirstOrDefault();
if (depProp == null)
return "";
return depProp.Name;
}
这篇关于查找导航属性的相关FK属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!