Programmatically use navigation properties in EF given the foreignkey

Answered Programmatically use navigation properties in EF given the foreignkey

  • Monday, January 14, 2013 11:16 AM
     
      Has Code
    In my project I use Entity Framework with ADO.NET. Suppose I have in Entity Framework the following classes:
    class Product
    {
       int Id { get; set;}
       string Name { get; set; }
       int TypeId { get; set; }
       int CategoryId { get; set; }
    }
    
    class Type
    {
       int Id { get; set; }
       string Name { get; set; }
    }
    
    class Category
    {
       int Id { get; set; }
       string Name { get; set; }
    }

    And then I have the navigation properties:

    1. hasCategory: from Category(Id) to Product(CategoryId), 1 to Many
    2. hasType: from Type(Id) to Product(TypeId), 1 to Many

    Therefore if I want to access a specific category name or type name of a product (given the context):

    int productId = 1;
    var categoryName = context.Products.Single(p => p.Id == productId).hasCategory.Name;
    var typeName = context.Products.Single(p => p.Id == productId).hasType.Name;

    Now if I have the property name I can get the property:

    string propertyName = "CategoryId";
    var property = GetType(Product).GetProperty(propertyName)

    Given the property I want to know if there is any way to get the matching navigation property (either hasCategory or hasType) in order to get the name. Oterwhise I do not know in which of the two classes Category and Type I should look for.

    • Moved by CoolDadTxMVP Monday, January 14, 2013 2:53 PM EF related
    •  

All Replies

  • Wednesday, January 16, 2013 7:24 AM
    Moderator
     
     Answered Has Code

    Hi Trinakriae,

    Welcome to the MSDN forum.

    If you mean the context and CategoryId are give, please check if the following helps:

                using (var context = new tstContext())
                {
                    String propertyName = "CategoryId";
                    Product p = context.Products.First();
                    var name = p.GetType().BaseType.Name;
    
                    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
                    var workspace = objectContext.MetadataWorkspace;
                    var items = workspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace);
                    foreach (var item in items)
                    {
                        EntityType entityType = item as EntityType;
                        if (entityType != null)
                        {
                            if (entityType.Name == name)
                            {
                                var navigationProperties = entityType.NavigationProperties;
                                foreach (var navigationProperty in navigationProperties)
                                {
                                    Console.WriteLine("Navigation properties: {0}", navigationProperty.Name);
                                    var properties = navigationProperty.GetDependentProperties();
                                    foreach (var property in properties)
                                    {
                                        Console.WriteLine("Foreign Key: {0}", property.Name);
                                        if (property.Name == propertyName)
                                        {
                                            Console.WriteLine("Matched!");
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Console.ReadLine();
                }
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.