Regenerate on save

Nov 10, 2009 at 4:51 PM

Hi all,

Is it a regular behaviour for my LINQ to SQL generator to regenerate all my files everytime I get to save my diagram? I had in mind that I would manually regenerate them by clicking "Run Custom Tool"... 

Is there a way to disable this functionality?


Nov 10, 2009 at 5:35 PM

Just adding: it regenerates (and reopens all files) everytime I:

  1. Save the dbml file
  2. Save the .tt template file

For someone who is concerned about saving the work as it evolves this is a bit annoying. 


Nov 10, 2009 at 5:41 PM
Edited Nov 10, 2009 at 6:17 PM

Marco --

Here is my observation of such matters.

Certain enduser-generated actions in VS.NET 2008 Standard IDE cause the "Run Custom Tool" to run "automatically", without the enduser having to click the "Run Custom Tool" command.

I think saving a diagram is one such action-- so, to answer your question-- "yes, I think it is regular behaviour".

It seems to me that editing a custom T4 template file (*.TT file) in my project also triggers a "Run Custom Tool" operation, for that TT file. Since T4 terminology and T4 Toolbox terminology is a bit odd to me, I should expand by saying-- I use the development model whereby a File.TT calls a Generator.TT file which calls a Template.TT file approach. My Generator.TT file and my Template.TT file are generic, the same for all projects, and the File.TT is project-specific. So when I edit my File.TT file, that causes "Run Custom Tool" to run for that set of T4 files. (I hope that less confusing rather than more. Ug. Terminology.)

As far as disabling such an IDE behaviour, there seems be no built-in way. However, given that the IDE is programmatically customizable, I think that one could write an addin or plugin or macro to hook that event and handle it in a custom way. I would not, however, recommend such a customization to core functionality as the Designer, given that doing so would likely cause other issues in dependent events and built-in IDE "automation", etc.


Thank you.

-- Mark Kamoski

Nov 10, 2009 at 5:45 PM

Thanks a lot, Mark. 

I'll try to save my files less frequently, then. hehehe

Nov 11, 2009 at 11:41 AM
Edited Nov 11, 2009 at 12:55 PM


I have changed the LINQ to SQL generator to generate output files every time the .dbml file is saved in version 9.10 of the toolbox. Before this version, you had to remember to run it manually. New behavior matches the built-in MSLinqToSqlGenerator and I made the change after receiving complaints about the difference from one of the teams using the previouis version of this generator. The problem they were frequently getting into is modifying the .dbml file and forgetting to regenerate the files.

The current behavior is hard coded in the LINQ to SQL generator, which associates the new T4ScriptFileGenerator custom tool with the .dbml file to trigger the code generation. I am hoping we will create an IWizard extension to make this happen when the dbml file is first added to the project. Once that is done, you will be able to remove the T4ScriptFileGenerator custom tool and run the code generation manually by "Running Custom Tool" on the .tt file.

What did you mean by saying that "it reopens all files"?


Nov 11, 2009 at 1:46 PM

Hi Oleg... 

I mean that all sql files are closed and right after hiting "save" visual studio reopens all of them. 

This is before hitting save (only my dbml file is open):

This is after hitting Save or pressing Ctrl+S or VS saving it automatically (when double clicking an error, for instance. Notice I've clicke the little arrow to show a bit of the files that are open):

I've browsed through VS's options but could find nothing regarding opening files upon modification.

Cheers, mate. 

Nov 11, 2009 at 11:33 PM

Hmm... this is strange. I hate to say it, but "It Works on My Machine". Are you using a C# project with dbml/.tt and a Database project for SQL scripts?

Nov 12, 2009 at 12:47 PM

C# project with DBML/.tt and a SQL 2005 database project for SQL.

It's funny cuz in a previous project with your toolbox I used a class library for the database project and guess what: it wouldn't open all those files at generation time. However, it wuoldn't update the database so I'd rather be annoyed by having all those files be opened than be annoyed by executing SQLs one by one manually. 

Thanks for your dedication, though, Oleg. =)

Nov 12, 2009 at 1:31 PM

No problem. I'll see if I can duplicate this once i get your source code with the foreign key problem.


Nov 12, 2009 at 3:54 PM


I found a way around.

As I don't need to regenerate things every single time I save my files, I just commented-out the .tt line for the Run() command, thus not having it run all the time.

	// Generate entity classes from a LINQ to SQL class model (.dbml file)
	LinqToSqlGenerator generator = new LinqToSqlGenerator();
	generator.DbmlFile = "NegociacaoCoops.dbml";
	generator.DatabaseProject = @"..\CoopsSql2005\CoopsSql2005.dbproj";

I know it's not the best way but it is absolutely better than having to watch files be opened and to close them manually everytime I save my diagram.

BTW: the foreign-key problem turned out to be a mix of two other problems: .sql leftover files for relations that were erased or changed and wrongly associated tables. These two are on me. Oleg did a great job, but Marcos didn't hehehe.