Clone this wiki locally
RoundhousE + NHibernate == Rebuild Your Database without leaving Visual Studio!
If you use NHibernate or Fluent NHibernate to define your domain model, you can use it to generate your sql scripts as well. There is a NuGet package RoundhousE.RefreshDatabase.FNH that brings some code to a console application (x86) to allow you to generate your SQL file and apply it to your local database immediately!## Notes You may not have the same setup on everyone's local machine for database instances. In cases like this, you can use SQL Configuration Manager set the same client alias for everyone to use. Then you use the alias in the server parameter (which will be configurable in the next version of RefreshDatabase.FNH). Right now it is `(local)` so you can set that as a client alias (if you don't already have that as an instance). See [setup an alias](http://www.mssqltips.com/sqlservertip/1620/how-to-setup-and-use-a-sql-server-alias/) and [alternate](http://geekswithblogs.net/twickers/archive/2009/12/08/136830.aspx).
- Create a console application (C# project). Make sure it is not set to Client Profile. It will not build.
- Make it x86.
- Use NuGet to install
- Set the start up program to
- Add a project reference to your application's DLL that has the Fluent Mappings (and/or HBMs)
- OPTIONAL: Add a project reference to your application's DLL that has the conventions (could be the same as the step above).
- Set up the following code (already at the top of RefreshDatabase.cs):
private static bool _isThisInitialDevelopment = true; //are you in greenfield or have you been to production? private static string _nameOfScript = "0001_CreateTables_NH.sql"; //"0002_AlterTables_NH.sql" //name of the script that will be created by nhibernate SchemaExport/SchemaUpdate - this will change the most over time as you move into maintenance //roundhouse information private static string _databaseName = "__REPLACE__"; //name of your database on your local default instance private static string _pathToSqlScripts = @"..\..\..\__REPLACE__"; //This is the path to your scripts folder where Up/Views/Functions/Sprocs are the next folder below. This is a relative path from bin\Debug. The three sets of parent folders already here should get it out of your project folder so you can traverse into the database project folder. private static string _repositoryPath = "__REPLACE__"; //The path to your source control repository. Used only for information sake. //restore private static bool _restoreDuringMaintenance = true; //you want to restore if you have a production backup that is small enough. Otherwise you get into a bit more advanced scenario that this package doesn't cover well private static string _pathToRestore = @"\\\\__REPLACE__.bak"; //this is the path to the restore file, likely on the network so everyone can get to it //Note: Add a reference to the project that has the Mappings/Conventions private static string _mappingsAssemblyPath = @".\__REPLACE__.dll"; //After adding a reference, the file will be in the build directory, so you can just add the name of the dll here. private static string _conventionsAssemblyPath = @".\__REPLACE__.dll"; //If you don't have a conventions assembly, just use the same DLL you just used for mappings.
- Whenever you have made mappings/conventions changes to your project or have updated database sql files, you can just run the project you created for instant local feedback.
- Right click on the project and select Debug -> Start new instance.
- Watch it run. If any errors occur, you will be able to see them in the console or in the database it was working with under the ScriptsRunErrors table.
- If the errors occur with the schema generation, hopefully that hangs in the console long enough for you to decipher what is wrong.
- If there are certain things you don't want SchemaExport/SchemaUpdate to generate, in the mapping class you can add this line in the constructor:
- If you are using NHibernate Mappings, you add this to the HBM file:
<class name="yourClassName" schema-action="none">
- There is a video and blog post showing this in action. Please see Refresh Database - Speed up your development cycles