查找导航属性的相关FK属性

时间:2011-02-13 23:49:29

标签: c# entity-framework-4

在运行时,我想查找EntityObject是否还具有给定NavigationProperty的外键属性。我在下面的两个步骤中有这个。我想这将需要一些元数据查询。

我不确定如何测试元数据类是否指向特定EntityObject类型的类型:即我在概念上而不是程序上知道EntityType实例和EntityObject实例之间的关系。

到目前为止,我有:

    /*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 "";

    }

1 个答案:

答案 0 :(得分:2)

我最后只是对ElementType的FullName进行文本比较

即:

n =>((RefType)n.ToEndMember.TypeUsage.EdmType).ElementType.FullName 
                               == targetProp.PropertyType.FullName)

ToEnd也是我所定位的属性,然后一旦我有了NavigationProperty,调用GetDependentProperties()似乎产生了我作为FK属性名称后的内容,即:

    public static String GetFKPropertyName(this NavigationProperty property)
    { 
        var depProp = property.GetDependentProperties().FirstOrDefault();

        if (depProp == null)
            return "";

        return depProp.Name;

    }