tmpl customizations

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for Oracle
Post Reply
travis_thelen
Posts: 20
Joined: Thu 14 Mar 2013 15:39

tmpl customizations

Post by travis_thelen » Thu 04 Feb 2021 20:28

I would like to add a custom attribute to my class properties that have a related navigation entity property. For example, I have a Car class with an EngineId property and the related navigation Engine property.

I would like to decorate EngineId as follows:

[CustomNavigationPropertyName("Engine")]
public virtual int EngineId
{
get;
set;
}

I am in the GenerateProperty(EntityProperty property, EntityClass cls, string currentClassName) tmpl method.

What is the code to check if the property is in an FK association, and, if so, what is the property name for the navigation entity?

Thanks,
Travis

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: tmpl customizations

Post by Shalex » Fri 05 Feb 2021 16:19

You can add a custom attribute via the interface of Entity Developer:
a) navigate to Model > Settings > Attributes > select the assembly with the needed attributes and make sure that the required attributes are checked in the window below, press OK
b) select the attribute in the Attributes collection of a particular class property

If you want to modify a template, specify:
1) the name of your model (e.g.: Devart Entity Model, *.edml)
2) the name of the template (e.g.: DbContext template)

travis_thelen
Posts: 20
Joined: Thu 14 Mar 2013 15:39

Re: tmpl customizations

Post by travis_thelen » Fri 05 Feb 2021 16:40

I would prefer to make this change by modifying the template.

I am using "Entity Framework DbContext template for Devart Entity Developer C# code generation" and my model is called Mosaic.edml.

Thanks,
Travis

travis_thelen
Posts: 20
Joined: Thu 14 Mar 2013 15:39

Re: tmpl customizations

Post by travis_thelen » Tue 09 Feb 2021 16:58

Any suggestions?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: tmpl customizations

Post by Shalex » Wed 10 Feb 2021 22:28

travis_thelen wrote: Thu 04 Feb 2021 20:28 What is the code to check if the property is in an FK association, and, if so, what is the property name for the navigation entity?
Here is the code:

Code: Select all

    foreach (EntityAssociation association in property.ParentClass.Model.Associations) {
      foreach (EntityProperty associationProperty in association.Child.Properties) {
        if (associationProperty == property) {
#>
        [CustomNavigationPropertyName("<#= codeProvider.GetValidIdentifier(association.Parent.Name) #>")]
<#+
        }
      }
      foreach (EntityProperty associationProperty in association.Parent.Properties) {
        if (associationProperty == property) {
#>
        [CustomNavigationPropertyName("<#= codeProvider.GetValidIdentifier(association.Child.Name) #>")]
<#+
        }
      }
    }
Our documentation doesn't include a reference for classes used in the templates, but there are some tips:
  • https://www.devart.com/entitydeveloper/ ... lates.html
  • use IntelliSense feature in our T4 Editor (completion list appears when you type the '.' character)
  • you can switch to the model in design time to read a description of a particular property in the Properties window
    general T4 reference: https://docs.microsoft.com/en-us/visual ... ew=vs-2019
  • decompiling
    C:\Program Files (x86)\Common Files\Devart\EntityDeveloper\EntityDeveloper.Common.dll
    C:\Program Files (x86)\Common Files\Devart\EntityDeveloper\EntityDeveloper.Orm.Common.dll
    C:\Program Files (x86)\Common Files\Devart\EntityDeveloper\EntityDeveloper.Orm.EntityFramework.dll
    may help you to get acquainted with the needed classes and their functionality

Post Reply