Mathematics
Mathematics

Reputation: 7618

Get all entities and their navigational properties

I am trying to get all entities (models) and then their navigational properties which are of type ICollection only, but it doesn't seem to be working. This is what I tried so far:

foreach (var propertyInfo in new CompanyDBContext().GetType()
                    .GetProperties(
                            BindingFlags.Public
                            | BindingFlags.Instance))
{
    Console.WriteLine(propertyInfo.Name);

    //var entity = DbSet<propertyInfo.Name>
}

I am able to get all models I have using code above, but how can I get all navigational properties which are of type ICollection next?

Upvotes: 0

Views: 1240

Answers (1)

Adnan Umer
Adnan Umer

Reputation: 3689

You have to first get type of all entities from DbContext. Any property of type DbSet<> defined within DbContext is actually enclosing an entity. You need to grab that instead.

private static readonly Type DbSetType = typeof(DbSet<>);
private static IEnumerable<Type> GetAllEntityTypes(Type contextType)
{
    return contextType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Select(p => p.PropertyType)
        .Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == DbSetType)
        .Select(t => t.GetGenericArguments()[0]);
}

And to get Navigation properties from Entity type, here is a helper method that is modified version of https://stackoverflow.com/a/27124251/2509344

private static readonly MethodInfo CreateObjectSetMethodInfo = typeof(ObjectContext).GetMethod("CreateObjectSet", new Type[0]);
private static readonly Type CollectionType = typeof(ICollection<>);
private static IEnumerable<PropertyInfo> GetNavigationProperties(DbContext context, Type entityType)
{
    var objectContext = ((IObjectContextAdapter)context).ObjectContext;
    var createObjectSetMethod = CreateObjectSetMethodInfo.MakeGenericMethod(entityType);
    var entity = createObjectSetMethod.Invoke(objectContext, new object[0]);

    var entitySet = (EntitySet)entity.GetType().GetProperty("EntitySet").GetValue(entity);
    var elementType = entitySet.ElementType;
    return elementType.NavigationProperties.Select(p => entityType.GetProperty(p.Name))
        // Filter Properties that are of type ICollection
        .Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == CollectionType);
}

And to finally get all navigation properties of entities defined inside DbContext:

var context = new CompanyDBContext();
var entities = GetAllEntityTypes(context.GetType());
foreach (var entity in entities)
{
    var navigationProperties = GetNavigationProperties(context, entity).ToList();
}

Upvotes: 1

Related Questions