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.
These are the necessary steps (install-path is currently C:\Program Files\SubSonic\SubSonic 2.1 Final)
- Install SubSonic
- Modify web.config to include the SubSonic configuration section
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