Altering DbContext template
Posted: Mon 24 Oct 2016 10:08
Hi
For years now I have been using your product and altering the DbContext template to produce the results that I'm after. This has always been with the VB templates however.
I am now starting to teach myself to work in C# and am having a certain degree of difficulty altering the template.
At present all I really want to do is implement some boiler plate code for Implementing IDataErrorInfo and to that end I have the following in the section that deals with generating separate files per class.
<#
}
foreach (EntityClass cls in _namespace) {
if (FilePerClass) {
string rootFileName = ModelNameAsFilesPrefix ? baseFileName + "." + cls.Name : cls.Name;
if (GeneratePartialClass) {
output.Extension = ".cs";
output.PushOutputRedirection(EntitiesOutput, "", rootFileName, OverwriteMode.None);
GenerateFileHeader(false);
#>
namespace <#= _namespace.Key #>
{
<#= codeProvider.FormatClassAccess(cls.Access) #> partial class <#= codeProvider.GetValidIdentifier(cls.Name) #>:IDataErrorInfo
{
#region "Fields"
#endregion
#region "Properties"
#endregion
partial void OnCreated()
{
// Add custom initialisation stuff here
}
#region "IDataErrorInfo Implementation"
public string this[string columnName]
{
get
{
string result = string.Empty
Switch (columnName)
{
<# foreach (EntityProperty property in cls.Properties) #>
case "<#= codeProvider.GetValidIdentifier(property.Name) #>":
result = "<#= codeProvider.GetValidIdentifier(property.Name)#> is required to have a value";
break;
return result;
}
}
}
public string Error
{
get
{
//Add implementation here
}
}
#endregion
}
}
When I try to save the model (and hence generate all the classes I'm getting an error that tells me 'The name 'property' does not exist in the current context'.
I've probably doe something stupid in relation to getting the syntax correct as it's sufficiently different to the VB that I'm used to to make altering the templates more of a challenge. Could you tell me what I've done incorrectly, and provide an example of how it ought to be done?
Thanks
Dom
For years now I have been using your product and altering the DbContext template to produce the results that I'm after. This has always been with the VB templates however.
I am now starting to teach myself to work in C# and am having a certain degree of difficulty altering the template.
At present all I really want to do is implement some boiler plate code for Implementing IDataErrorInfo and to that end I have the following in the section that deals with generating separate files per class.
<#
}
foreach (EntityClass cls in _namespace) {
if (FilePerClass) {
string rootFileName = ModelNameAsFilesPrefix ? baseFileName + "." + cls.Name : cls.Name;
if (GeneratePartialClass) {
output.Extension = ".cs";
output.PushOutputRedirection(EntitiesOutput, "", rootFileName, OverwriteMode.None);
GenerateFileHeader(false);
#>
namespace <#= _namespace.Key #>
{
<#= codeProvider.FormatClassAccess(cls.Access) #> partial class <#= codeProvider.GetValidIdentifier(cls.Name) #>:IDataErrorInfo
{
#region "Fields"
#endregion
#region "Properties"
#endregion
partial void OnCreated()
{
// Add custom initialisation stuff here
}
#region "IDataErrorInfo Implementation"
public string this[string columnName]
{
get
{
string result = string.Empty
Switch (columnName)
{
<# foreach (EntityProperty property in cls.Properties) #>
case "<#= codeProvider.GetValidIdentifier(property.Name) #>":
result = "<#= codeProvider.GetValidIdentifier(property.Name)#> is required to have a value";
break;
return result;
}
}
}
public string Error
{
get
{
//Add implementation here
}
}
#endregion
}
}
When I try to save the model (and hence generate all the classes I'm getting an error that tells me 'The name 'property' does not exist in the current context'.
I've probably doe something stupid in relation to getting the syntax correct as it's sufficiently different to the VB that I'm used to to make altering the templates more of a challenge. Could you tell me what I've done incorrectly, and provide an example of how it ought to be done?
Thanks
Dom