First Steps with SubSonic

I have already written a couple of posts regarding code generation, which described how to generate C# or T-SQL code based on a database schema or table data.

My recent browsing expedition led me to T4 and SubSonic. After downloading and installing I found this introductory post on how to use SubSonic in your project.

These are the necessary steps (install-path is currently C:\Program Files\SubSonic\SubSonic 2.1 Final)

As you need to add a provider in the SubSonicService section, make sure the chosen name is not a C# keyword, as this will cause a compilation error.

  • Add a reference to the SubSonic.dll C:\install-path\SubSonic directory
  • Call SubSonic SubCommander from the command line:
C:\install-path\SubCommander\sonic.exe generate 
    /config \path-to-config /out \path-to-generated-source

This command creates a C# file for each table and view in your database, and wrappers for stored procedures.

Using this code, one can generate SQL statements disguised as C# code. I plan to leave my existing SQL code as it is, and write new functionality with the SubSonic framework.

I noticed that inserting records (via new Table() and .Save()) will overwrite the columns’ default values with NULL even if you do not set their values in code. You have to call the table object constructor with a true parameter to generate default values.

I still need to figure out the following questions:

  • How to run a stored procedure on a newly opened connection (connections are opened and closed automatically)
  • How to use SubSonic in conjunction with SqlDataSources for FormViews and GridViews

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: