Prevent T4 template from deleting existing files?

No, you can't. The T4 Templates generates the code each time they are triggered and perform the code. And before that all files get deleted.

But what is your problem with that? You custom code should be placed in partials so it doesn't matter if the the other partial get's regenerated.

Thank you for your answer. Of course the custom classes are in separate partial classes, but the partials shall be created each time the t4 template is run and if the edmx has been updated by the user (a new table was added), the existing classes shall be untouched and the new validator class for the newly added databasetable shall be generated (and not handcrafted by the user). That is what I want to have, but maybe providing a snippet or a template is less time consuming for this purpose ;-).

Thanks! – HarryK May 10 at 4:34 Yes, that seems to be the better Solution for your needs, cause you want be able to tell T4 to spare already generated files. – sra May 10 at 4:49 I think I will do this in a snippet/template way - I have not time to loose :-).

Thank you for your answers! – HarryK May 10 at 5:39.

T4 generates the code and you should not touch generated code. If you use T4 template just to create files with some names you are using it wrong. You can either create real T4 template which will generate content of your file as well or you should not use T4 template and create files manually.

Thank you for your answer - I will keep that in mind. Nothing is wrong with recreation except: I do not want this. If you do not care about the content of the file (of course, the file would get some generated logic) the challenge for me was not just to create some files, the challenge was: CAN I prevent the files from being regenerated each time if I do not want this?

I guess I better go with snippets and templates and MAYBE T4 is - and you might be right - not the right tool for this particular purpose. Thanks! – HarryK May 10 at 5:38.

I am in the exact same situation. I want to generate buddy classes for data annotation. I don't want to put data into the edmx file and alter my templates to add the correct annotations based on the edmx data.

That would take too long. The easiest solution is to generate a buddy class and set it so it doesn't regenerate every time. In this case efficiency is more important than the convention that T4 classes should always be regenerated.

Dane Morgridge figured out a clever way to do this. He checks to see if the file already exists. If it does he reads it in and writes it back out the way it was.

If it doesn't he renders his template. Check out DbContext Templates\IRepository.tt. Https://github.Com/danemorgridge/efrepo Here are the relevant sections.

String OutputFile(string filename) { using(StreamReader sr = new StreamReader(Path. Combine(GetCurrentDirectory(),filename))) { string contents = sr.ReadToEnd(); return contents; } } if(!DoesFileExist(entity.Name + "Repository. Cs")) { fileManager.

StartNewFile(entity.Name + "Repository. Cs"); } else { fileManager. StartNewFile(entity.Name + "Repository.

Cs"); this. Write(OutputFile(entity.Name + "Repository. Cs")); }.

FileName = "C:\\Program Files (x86)\\SourceGear\\DiffMerge\\DiffMerge.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions