Disabling navigation properties

Thread is closed for posting
2 posts, 0 answers
  1. Andrew
    Andrew avatar
    8 posts
    Member since:
    Jan 2013

    Posted 02 May 2013 Link to this post

    I am using a repository pattern in a database-first environment in my data layer, and want to route all queries through that. Therefore I'd like to disable navigation properties from being generated on my model classes. Currently I'm just editing the association through the designer and unchecking the box for the navigation property, but I'm looking for a more automated way of doing this when the classes are generated (so when a new table is added this step doesn't have to be done).

    I figure I can customize the T4 templates, but what would I check to determine if the property being rendered is a navigation property or not?

  2. Yordan
    Yordan avatar
    39 posts

    Posted 06 May 2013 Link to this post

    Hi Andrew,

    The desired functionality could be achieved by modifying the template located in (OpenAccess ORM Install Folder)\dsl(Version of Visual Studio)\CodeGenerationTemplates\CSharp\Includes\Specific.ttinclude. The subfolder if Visual Studio 2012 is used will be dsl2012 and dsl2010 if Visual Studio 2010 is used. The generation of navigational properties will be skipped if the following code is inserted at line 219 of the aforementioned file:
    if (propertyType.StartsWith("IList") || this.DomainModel.Bridge.MetaModel.PersistentTypes.Any(p => p.Name == propertyType))

    After the model is regenerated with this code template no navigational properties in the generated classes will be available. For example, a category class will not have collection of products and product type will not have a property that will reference the category type.

    When modifying code templates it is good approach to follow best practices described in this article.

    Please find the attached file as a modified version of Specific.ttinclude so the generated code using this template wont have navigational properties.

    If there are any difficulties using the modified template or if the achieved behavior is not suitable in your scenario do not hesitate to contact us again.

    All the best,
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Back to Top