There are two approaches which you could use in order to initialize the string properties of your persistent classes. One is to modify our code generation templates
, the other is to extend the persistent classes with a customized parameterless constructor
. In my answer I will detail both of these approaches.
Customizing the code generation templates:
From Code Generation Settings window of both the Model Settings dialog
or the New Domain Model wizard
you can copy the default templates locally to your project in order to modify them. Use the Copy Default To...
button to do so. After that, under the Includes_ver.2
folder you can find the PropertiesGenerator
template. In it, you can modify the GenerateFieldForProperty
method to auto-initialize the string properties for your domain classes. The following code snippet is an example:
propertyType = property.ToTypeString();
" = String.Empty"
" = new List<"
+ property.Type +
<#= fieldModifier #> <#= propertyType #> <#= property.FieldName #><#= initialValue #>;
This approach is applicable when you need the string properties of multiple domain classes to be auto-initialized with the same value.
Using custom parameterless constructor:
The persistent classes of your model are generated as partial
so their definition can be done over multiple code files. You could create a new code file for each class where you would like to have the string properties initialized and define a constructor with no parameters which initializes the string properties. This approach requires more manual modifications but it does provide more control over which of the string properties should be initialized and how.
I have attaches a sample project which illustrates both approaches.
I hope this helps. Should you have additional questions or need assistance, feel free to contact us again via our Ticket System or post in our forums again.
OpenAccess ORM is now Telerik Data Access
. For more information on the new names, please, check out the Telerik Product Map