Caching of database schema in CodeSmith

So you've build a template (or using one) for CodeSmith that uses SchemaExplorer.DatabaseSchema class - the one that enables you browsing through database schema with ease (through a nice API) and a killer feature when dealing with databases. There is one catch though:

The first time you run a template it takes a little longer to execute while the next time (if you didn't change it in between). The reasons for this behaviour are two:

  • CodeSmith Professional does template code caching (it compiles the template only once if not closed or changed). This is good for batching - it saves you time.
  • Perhaps less know is the fact that it does caching of database schema, too.

The later fact might be a problem. Imagine you run a template that does database modifications (creates a SQL that you run). Now, the second run will produce the exact same script not taking in account that database was meanwhile changed. Don't worry though as solution is simple. Just invoke this method somewhere at the beginning of the template code and you are fine:

[your instance of DatabaseSchema].Refresh();