SubSonic is a great framework! SubSonic is a great Object Relational Mapping Framework based on ActiveRecord (same as Ruby on Rails) for .Net 2.0. Simply point it at a database and it will create your all the code for connecting to your database.
In this post, i’m just going to quickly discuss how you can get SubSonic to generate the classes for your database. The first thing I always do is add a reference to “C:Program FilesSubSonicSubSonic 2.0.3SubCommandersonic.exe” in my External Tools dialog. This way, whenever I need to refresh my database schema, I can select the menu item and it will go off and create the classes for me.
A subsonic item will then be listed in your Tools menu.
The next task is to configure your App.Config
There is three main sections, first the configSections which must appear first (directly under configuration). Next, we have the connection strings for the databases. Finally, we just setup the SubSonic service, giving it a name (CustomerDatabase) and the connection string to use. There are more options we can add here, but to keep things simple I will just give it the namespace I want my generated code to live in.
Next, add a reference to the SubSonic.dll assembly. This can be found at “C:Program FilesSubSonicSubSonic 2.0.3SubSonic.dll”. You also need to add a reference to System.Web and System.Configuration.
Finally, your assembly needs to be strongly named as SubSonic.dll is now also strongly named. You can do this in the signing tab in the project properties in Visual Studio. This is a fairly new thing, however does solve problems with the GAC.
That’s it. Select the external tools menu item and SubSonic will go off and do its thing. You will then have a folder called Generated in your project directory. You need to click right and include the folder for it to be compiled with your application.
You can now write the code below, upon calling Save, the customer is saved into the related table!
Customer c = new Customer();
c.Name = “Test”;
c.Email = “[email protected]”;
I do love SubSonic, I think it’s great. The fact you can get up and running so quickly is great.