Page 1 of 1

Altering DbContext template

Posted: Mon 24 Oct 2016 10:08
by dom
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

Re: Altering DbContext template

Posted: Mon 24 Oct 2016 16:59
by Shalex
Foreach / for / if / etc in C# require open and close brackets (and Entity Developer looks for them to set a visibility area for the corresponding variable):

Code: Select all

<#
  foreach (EntityProperty property in cls.Properties) {
#>

<#
  }
#>

Re: Altering DbContext template

Posted: Wed 26 Oct 2016 07:15
by dom
Many thanks

Curly braces were always what put me off learning c# in the first place!

With a deal of experimentation I've managed to figure out how the template should be structured and my final model is exhibiting the correctly structured code that I need. Thank you.

Dom