Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was actually able to determine the foreign key properties by using the <a href="http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.navigationproperty.getdependentproperties.aspx" rel="noreferrer">GetDependentProperties</a> method of the <a href="http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.navigationproperty%28v=vs.110%29.aspx" rel="noreferrer">NavigationProperty</a> class. </p> <p>Here is the code that I used (more or less) to get what I needed:</p> <pre><code>object[] GetForeignKeyPropertyValues&lt;TEntity, TRelatedEntity&gt;(TEntity entity, Expression&lt;Func&lt;TEntity, TRelatedEntity&gt;&gt; navigationProperty) { if (entity == null) return new object[] { }; // Find the entity metadata in the object context. // (Assume you have access to the DbContext through the property CurrentDbContext.) var objectContext = (CurrentDbContext as IObjectContextAdapter).ObjectContext; var metadataNamespace = ObjectContextAdapter.GetType().Namespace; var entityIdentity = metadataNamespace + "." + typeof(TEntity).Name; // HACK: This seems to work to retrieve the EntityType for an entity. var entityMetadata = objectContext.MetadataWorkspace.GetItem&lt;EntityType&gt;(entityIdentity, DataSpace.CSpace); // TODO: Verify that the entity metadata was found. // Get the navigation property metadata by parsing the name from the navigation property expression. var navigationPropertyName = GetPropertyName(navigationProperty); var navigationPropertyMetadata = entityMetadata.NavigationProperties.FirstOrDefault(np =&gt; np.Name == navigationPropertyName); // TODO: (JMB) Verify that the navigation property metadata was found. // Extract the foreign key columns from the navigation property. var foreignKeyPropertyMetadatas = navigationPropertyMetadata.GetDependentProperties(); // Create property getters for each foreign key property. var foreignKeyPropertyGetters = foreignKeyPropertyMetadatas .Select(propertyMetadata =&gt; MakePropertyGetter&lt;TEntity&gt;(propertyMetadata.Name)) .ToArray(); // Execute the foreign key property getters to get the foreign key property values for the specified entity. var foreignKeyPropertyValues = foreignKeyPropertyGetters .Select(propertyGetter =&gt; propertyGetter(entity)) .ToArray(); return foreignKeyPropertyValues; } static string GetPropertyName&lt;TEntity, TProperty&gt;(Expression&lt;Func&lt;TEntity, TProperty&gt;&gt; navigationProperty) { var lambda = navigationProperty as LambdaExpression; var member = lambda.Body as MemberExpression; return member.Member.Name; } static Func&lt;TEntity, object&gt; MakePropertyGetter&lt;TEntity&gt;(Type entityType, string propertyName) { var parameterExpression = Expression.Parameter(typeof(TEntity), "entity"); var propertyExpression = Expression.PropertyOrField(parameterExpression, propertyName); var lambdaExpression = Expression.Lambda(propertyExpression, parameterExpression); var lambdaFunction = lambdaExpression.Compile(); return (Func&lt;TEntity, object&gt;)lambdaFunction; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload