C# and Visual Basic Code Generation

Entity Developer incorporates a large number of innovative features which makes it a perfect C# and Visual Basic code generation tool even for the most demanding developers. Using Entity Developer, you can fine-tune the code generation process according to particular needs by flexibly using different types of new templates that are shipped with the application.

Common C# and Visual Basic Code Generator Functionality

Entity Developer code generator lets you use templates to generate source code for ORM models. Entity Developer has a very flexible code generation system, based on templates, written in T4-like Template Language.

Entity Developer Templates

Predefined Templates

Entity Developer has a template gallery with various predefined templates for different use cases. Templates can be added to your model either when you create it in the Create Model wizard or you may add them later via Add Template dialog box.

Entity Developer models can have a freely defined set of templates. This allows generating non-connected code against different templates. The Model Explorer window provides a convenient means for managing templates.

Templates for C# and VB code generator

Storing Templates

Templates can be stored both within the model and in Entity Developer resources.

 

The advantages of the first approach are the following:

  • the template is stored in the model and can be stored in a source control system;
  • the template can be moved to other computers or into different folders;
  • the template is editable.

The advantages of the second approach are the following:

  • If you need to frequently update versions of Entity Developer, you can always be sure that your template remains actual.
  • It is possible to add templates to the model both at the creation stage and at any other moment using a handy editor functionality.
  • the template is editable.


To add a template stored on the disk to the model, you should use the Add Existing command in the editor.

Code Generation Management

Entity Developer manages all the process of code re-generation, and you don't need need to do anything special to generate code for your model. When you use Entity Developer as a Visual Studio add-in, code is generated for a model every time when the changed model is saved, so that you always have all model updates available in the code.

When developing a model that at certain moments can become invalid, or working with a very large model, for which code generation takes time, you may disable template generation by using the Enabled property of a model template.

Note that when you use Entity Developer as a separate application, code is generated only when you click the Generate Code button or press F7. It is not re-generated when you save the model.

Templates for C# and VB code generator in the Model Explorer window

Code Generation Configuration

Template Properties

Code generation can be configured with the template properties, which are declared with the property directive in the templates.

Properties declared in this way are then displayed in the properties window and can be easily edited. In addition to standard properties of the property directive, Entity Developer supports the definition of a group and description.

Defining template properties

 

Output Configuration

All templates have the FilePerClass option as well as the EntitiesOutput and ContextOutput. EntitiesOutput and ContextOutput provide a capability that slightly differs in Visual Studio and in the stand-alone version, as described below.

If you are using Entity Developer as a Microsoft Visual Studio add-in, you can define the project and project folder to store generated code for contexts and entities of your model. In the Entity Developer stand-alone application, it is possible to define folders, in which generated code for entities and contexts is to be stored.

This set of options makes the development of multilayer applications easier and more convenient. For example, you can easily create the following structure of the application by setting the following options.

Customizing output

Custom Attributes Support

All templates support the generation of .NET attributes that are defined through a convenient dialog in the designer. In the designer, for example, you can define the System.ComponentModel.DataAnnotation attributes that will be generated in your classes.


Custom attributes

Refactoring Integration

Entity Developer supports integration with Visual Studio refactoring that enables renaming of model objects. For example, when renaming an entity, refactoring will ensure that any references to that class are renamed across your solution. Refactoring is performed when you save your project and is applicable to namespaces, classes and complex types as well as their properties, enums and their members; for LinqConnect, Telerik Data Access, and Entity Framework models it also applies to the context class and its properties.

Advanced Capabilities

Custom Templates

At some stage, the capabilities of the existing templates might seem insufficient and you may want to create new templates (based on the blank template) or to extend the existing ones (for that purpose, you can use the Copy-to-model option when adding a template or make the currently added template refer to a template in the resources).

Custom templates can be easily made shared using the "make shared" option. This allows using custom templates in other models. Shared templates are displayed in the Custom category and are stored in the C:\Program Files\Common Files\Devart\EntityDeveloper\Shared Templates folder.

Template Editor

Templates are created and edited with convenient editor, which provides context-sensitive code completion, error check, syntax highlighting and other features to speed up your work. All errors and warnings are automatically displayed in the Error List window. The Error List displays an error or warning message and the error location. You can easily navigate to the line that caused the error by double-clicking the error in the Error List window. Thus you may find and fix template errors quickly.


T4 editor

Advanced Output Configuration

In custom templates you can use the following additional parameters with the PushOutputRedirection function that can be called inside the template code:

Extended Properties

At certain stage of development, the existing properties of model objects might not be sufficient. Using Entity Developer you can extend the property lists for objects in the model. These extended properties are either declared in the template, or defined in the model, and then used in code generation. For example, you can add the Generate property to every class and then exclude some classes from generated code by changing the value of this property in the designer.


Extended properties