Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3961978
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:02:27+00:00 2026-05-20T03:02:27+00:00

At run time I would like to find whether an EntityObject also has a

  • 0

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.

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.

So far I have:

    /*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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:02:27+00:00Added an answer on May 20, 2026 at 3:02 am

    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)
    

    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;
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to know, in run-time in my code, how
How would one go about loading compiled C code at run time, and then
It looks like the run-time compiler doesn't support the same language as the command-line
How do I access an image during run time that I have added to
I have a solution to build a DLL with run-time type information enabled in
I would like to retrieve at runtime the values for the dialect and connection.driver_class
At run time I want to dynamically build grid columns (or another display layout)
I receive this Run-Time Check Failure upon the return in the following code. I
I need to at run time change the font of a List Control so
I'm trying to determine the asymptotic run-time of one of my algorithms, which uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.