I am working with a set of T4 script that generate partial classes for my entities in a EF application.
Because partial classes need to reside in the same assembly, the script resides in the same project as the entity classes. It also needs access to the compiled assembly when executing.
When an error occured, the script will fail with the output “ErrorGeneratingOutput”. This will cause the whole project to NOT compile, because the generated file is an .cs file, with (at that point of time) invalid content.
So thats a vicious dependency circle, which can only be broken if I manually remove the error message from the generated file, and then trigger the build.
If there was a way to suppress the error message (or replace it by an empty string), my life would be much easier.
So the question is: can i change the error handling of a t4 script?
In some cases, this problem can be solved easily.
In my case, the problem was about loading the DLL of the project the T4 script resides in. The assembly directive was placed in the top region of the script (line 5). So i changed the output extension to txt.
Then I placed the real output into another file using the EntityFrameworkFileManager.
When the error occured, that an assembly cannot be loaded, the ErrorGeneratingOutput message is printed to the default .txt file, where it does not produce a problem for the compilation. If the assembly can be loaded, the output is printed to the Output.cs file.
This way the project can be build after repairing the initial problem, and the developer doesnt have to take care about the ErrorGeneratingOutput problem, too.