Metadata API

5 posts, 1 answers
  1. Thorsten
    Thorsten avatar
    26 posts
    Member since:
    Apr 2010

    Posted 20 Jan 2013 Link to this post

    Hi,

    i've got an issue where i need get the definition of all entities, their relations and entity sets on runtime for an openaccess context.
    I read the metadata documentation, but it seems like the metadata container is more focused on the relational model than on the conceptual one.
    I'm able to retrieve the entities via the "PersistentTypes" and the relations via their corresponding navigation members.
    But on the entity sets, respectively their names and entity mapping, I'm stuck.

    Is there a way to get those informations via the Metadata-API or am i forced to mix reflection and metadata container calls in this case ?
    ( e.g. Type.GetProperties -> IQuerable<T> Property -> Map name of T to PersistentType -> etc.) 

    Thanks in advance

    Kind regards,

    Thorsten Klingert
  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 23 Jan 2013 Link to this post

    Hi Thorsten,

    I am not sure what you mean by "Entity Sets" and "Entity Mapping", can you be more detailed?

    Can you tell us what specific scenario you want to implement, so we could provide you with a solution? 

    If by Entity Set you mean a CLR Type that is mapped to persistent type, you can use Telerik.OpenAccess.RT.PersistentClassesRegistry from Telerik.OpenAccess assembly in this way:
    MetaPersistentType type = context.Metadata.PersistentTypes.FirstOrDefault();
    Type entityType = PersistentClassesRegistry.GetPersistenceCapableType(type.FullName);
     
    //Create an instance of corresponding type
    object instance = PersistentClassesRegistry.getInstance().newInstance(entityType, null);

    You should have in mind that using the "Members" property from context.Metadata.PersistentTypes gives you only a List<MetaMember> with members of the class, and didn't give you the members from base classes. If you want to take members from these classes you should use Telerik.OpenAccess.Metadata.MetadataWorker:
    var allMembers = MetadataWorker.GetAllMembers(metaPersistentType);

    Also MetaMember.Name gives you the name of the field and MetaMember.PropertyName gives you the name of the corresponding property.

    I am looking forward to hearing from you about the meaning of "Entity Sets" and "Entity Mapping" which you used, and provide your with better solution.

    Regards,
    Boris Georgiev
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  3. DevCraft banner
  4. Thorsten
    Thorsten avatar
    26 posts
    Member since:
    Apr 2010

    Posted 23 Jan 2013 Link to this post

    Hi Boris,

    thanks for the reply.

    I meant the property on the openaccess context that is generated for a persistent type.

    TB_Customer (Table) -> Customer (PersistentType) -> IQueryable<Customer> Customers  (EntitySet, OpenAccessContext)

    Inside VisualStudio the "OpenAccess Model Explorer" lists them under:
    OpenAccess Model Explorer -> Domain Context -> Entity Sets

    Is there a way to retrieve this listing via the Metadata API ?

    Basically i need to build up similiar view like the model explorer:

    Context
        -> EntitySet1
    -> Entity
         -> Property1 + Details
         -> Property2 + Details
    etc.


    Kind regards,

    Thorsten




  5. Answer
    Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 24 Jan 2013 Link to this post

    Hello Thorsten,

    In the metadata we don't keep information about the context. This information is only needed in design time to generate the classes, that's why it is not accessible from the metadata container

    As you suggest, you should combine a metadata container and reflection to map entity sets with entities.

    For your convenience I prepared the following code demonstrating that approach and generating the tree view that you want and printing it on the Console:
    01.using (EntitiesModel context = new EntitiesModel())
    02.{
    03.    Type contextType = context.GetType();
    04.    var propertiesInformation = contextType.GetProperties(BindingFlags.Public | BindingFlags.Instance).
    05.        Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(IQueryable<>));
    06. 
    07.    var allPersistentTypes = context.Metadata.PersistentTypes;
    08. 
    09.    Console.WriteLine("Context");
    10. 
    11.    //get name of the type
    12.    foreach (var propertyInfo in propertiesInformation)
    13.    {
    14.        //Get CLR Type from IQueryable property
    15.        var genericArgumentType = propertyInfo.PropertyType.GetGenericArguments().First().FullName;
    16.        Console.WriteLine("\n\n - IQueryable<{0}>", genericArgumentType);
    17. 
    18.        //Map CLR Type with persistentType
    19.        var persistentType = allPersistentTypes.FirstOrDefault(pt => pt.FullName == genericArgumentType);
    20. 
    21.        //get only navigation properties
    22.        var navigationProperties = persistentType.Members.Where(mem => mem is Telerik.OpenAccess.Metadata.MetaNavigationMember);
    23.         
    24.        Console.WriteLine("\n   - Navigation Properties\n");
    25.        foreach (var member in navigationProperties)
    26.        {
    27.            Console.WriteLine("     {0} - {1}", member.PropertyName, member.MemberType.FullName);
    28.        }
    29.         
    30.        //get only primitive properties
    31.        var primitiveProperties = persistentType.Members.Where(mem => mem is Telerik.OpenAccess.Metadata.MetaPrimitiveMember);
    32. 
    33.        Console.WriteLine("\n   - Primitive Properties\n");
    34.        foreach (var member in primitiveProperties)
    35.        {
    36.            Console.WriteLine("     {0} - {1}", member.PropertyName, member.MemberType.FullName);
    37.        }
    38.    }
    39.}


    I hope that helps. If any other questions arise, do not hesitate to contact us.

    Regards,
    Boris Georgiev
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  6. Thorsten
    Thorsten avatar
    26 posts
    Member since:
    Apr 2010

    Posted 24 Jan 2013 Link to this post

    Hi Boris,

    thanks for the example, I'll do it that way.

    Kind regards,

    Thorsten Klingert
Back to Top
DevCraft banner