You can also modify your T4 template (.tt file) to add a partial method called from the generated constructor. Then you can create your own partial class and implement the partial method and set the default value.
An excerpt from the T4 template, where the constructor is created, and then the partial method. Pay attention to the last three lines:
public <#=code.Escape(entity)#>() { <# foreach (var edmProperty in propertiesWithDefaultValues) { #> this.<#=code.Escape(edmProperty)#> = =code.CreateLiteral(edmProperty.DefaultValue)#>; <# } foreach (var navigationProperty in collectionNavigationProperties) { #> this.<#=code.Escape(navigationProperty)#> = new HashSet<<#=code.Escape(navigationProperty.ToEndMember.GetEntityType())#>>(); <# } foreach (var complexProperty in complexProperties) { #> this.<#=code.Escape(complexProperty)#> = new <#=code.Escape(complexProperty.TypeUsage)#>(); <# } #> SetDefaultValues(); } partial void SetDefaultValues();
This will result in a generated object having something like:
public Foo() {
Then in your partial class, you can simply add something like:
partial void SetDefaultValues() { this.SomeDate = DateTime.Today; }
Ecyrb source share