How Do You Determine the Parent Navigation Properties for an Entity Using Entity Framework

59 Views Asked by At

I have an Entity Framework 6 POCO and I want to obtain the list of parent navigation properties for that POCO. The code below is how far I have gotten. It seems to work but I can't confirm it will work in all cases.

For example, for POCO Book

class Book
{
   int AuthorId;
   int BookId;
        
   virtual Author TheAuthor;
}



// Here I pass in the EntityType for the Book POCO.  
// This should return new List<string> { "TheAuthor"}
public GetParentNavigationProperties(System.Data.Entity.Core.Metadata.Edm.EntityType entityType)
{
    
    return entityType.NavigationProperties
    .Where(np => np.GetDependentProperties().Count() > 0)
    .Select(np=>np.Name).ToList(); 
}

The code above is in part derived from the following post: Programmatically obtain Foreign keys between POCOs in Entity Framework 6

0

There are 0 best solutions below