Get collection types from EntityFramework ObjectContext

How to extract a list Typesfrom ObjectContext?

For example, I have an object context containing an object named "Bank" and an object called "Company". I want to get the EntityObject type from them.

How can i do this?

+3
source share
2 answers

I assume that at runtime you want to query the generated class ObjectContextto get a list of classes EntityObject. Then it becomes an exercise in reflection:

PropertyInfo[] propertyInfos = objectContext.GetType().GetProperties();
IEnumerable<Type> entityObjectTypes =
  from propertyInfo in propertyInfos
  let propertyType = propertyInfo.PropertyType
  where propertyType.IsGenericType
    && propertyType.Namespace == "System.Data.Objects"
    && propertyType.Name == "ObjectQuery`1"
    && propertyType.GetGenericArguments()[0].IsSubclassOf(typeof(EntityObject))
  select propertyType.GetGenericArguments()[0];

This code will find all public properties in the context of an object that has a type System.Data.Objects.ObjectQuery<T>where it Tis a subclass EntityObject.

+5

, ,

MetaModel.GetModel(objectContext.GetType()).Tables.Select(t => t.EntityType);
0

Source: https://habr.com/ru/post/1713569/


All Articles