Comments from Database

Thread is closed for posting
2 posts, 0 answers
  1. Saeed
    Saeed avatar
    1 posts
    Member since:
    Jun 2013

    Posted 25 Apr 2014 Link to this post


    I have Oracle Database with too many tables and all of tables and columns is commented.

    Is it possible to customize generate templates that generate class with xml comment from database table comment
    and properties with [DisplayName("comment from database column")] DataAnnotation?

    In the other hand I want query from Database data dictionary in the generation time.
  2. Boyan
    Boyan avatar
    100 posts

    Posted 29 Apr 2014 Link to this post

    Hello Saeed,

    To answer you question - yes, it is possible to open database connection and perform queries against the database in the code generation templates. In order to do so, you will need to use the Ado API of the Telerik Data Access.
    To demonstrate this, I will describe how you could generate custom xml comments or attributes based on the comments that you applied in your database. Please find the resulting modified code generation templates attached.
    I assume that the database objects has been commented as described in this Oracle documentation article.

    I have applied this workflow to extend the code generation templates:

    1. Copy the Telerik Data Access default code generation templates locally. Once you have copied them you will find that a file named is available. This file contains all changes that are required. Please refer to the Copy the Templates Locally section of this documentation article for more information. 
    2. Create a class to be used for querying against the database. In the sample templates, this class is named DatabaseCommentsRetriever and is located at line 133. In short, this class uses a connection string to create a Telerik Data Access context and then uses this context to retrieve comments for tables or for table rows. You could modify the predefined sql queries to accommodate for scenarios specific for your implementation. Also you will need to update the connection string - at line 178.
    3. Inherit from the classes responsible for the generation of the attributes and the entities. This classes are named AttributesTranslator and EntityClassesTranslator. You could see the resulting new classes respectively at line 59 and at line 106.
    4. The overridden methods in the CustomAttributesTranslator works as follows:
    a. Retrieve the attributes collection by calling a base method.
    b. Use DatabaseCommentsRetriever​ to retrieve the comments for either a table or a table row from the database.
    c. Create new custom attribute and add it to the attributes collection.
    5. The overridden methods in the CustomEntityClassesTranslator work similarly:
      a. Retrieve a CodeClass (or a CodeProperty) by calling a base method. Those classes represent code elements in the generated code.
    b. Use DatabaseCommentsRetriever​ to retrieve the comments for either a table or a table row from the database.
    c. Update the Summary property with the information retrieved from the database.
    6. Plug the newly create classes in the code generation. In is done by passing them as parameters in the ModelContext class constructor. This is located at line 50 and at line 51.

    At this point, when you re-save the .rlinq file causing regeneration of the entities, you will find that they were updated with the corresponding information retrieved from the database. 

    Should you have any more questions or need any further assistance, do not hesitate to contact us again.

    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
Back to Top