Geeks With Blogs

Scott Kuhl Warning: I may have no idea what I am talking about!

SubSonic requires a minimal amount of configuration to get going. 

SubSonic Configuration Section

Start by adding a SubSonic configuration section inside the configuration tag in the web.config file.  This default configuration should work for most projects.

<configSections>

<section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" allowDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="false"/>

</configSections>

Data Provider

Second, you will need to setup a data provider.  Three are currently supported by SubSonic: SQL Server, MySQL and Enterprise Library.  The following are sample configurations for each of these.  This information is also added inside the configuration tag.

<SubSonicService defaultProvider="SqlDataProvider" spClassName="SPs" fixPluralClassNames="true">

<providers>

<add name="SqlDataProvider" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="NorthwindConnection"/>

<add name="ELib2DataProvider" type="ActionPack.ELib2DataProvider, ActionPack" connectionStringName="NorthwindSQL"/>

<add name="MySqlDataProvider" type="ActionPack.MySqlDataProvider, ActionPack" connectionStringName="NorthwindMySQLConnection"/>

</providers>

</SubSonicService>

There are five values that can be set in the SubSonicService tag.

  • defaultProvider - Multiple providers can be setup in the configuration.  This value indicates which provider to use.
  • fixPluralClassNames - SubSonic can remove the plural characters from the end of table names to make class names more consistent.  For example, the Products table would produce a Product class.
  • generatedNamespace - By default all classes generated will be part of the project's global namespace.  This value overrides that behavior and includes all classes in the given namespace.  For example, by setting this to Northwind you would get Northwind.Product.
  • spClassName - Each stored procedure will generate a method of the same name.  The value will be the class these methods are included under.  For example, by setting this to SPs the CustOrderHist stored procedure would be SPs.CustOrderHist.  Using the above namespace example in conjunction with this value would produce Northwind.SPs.CustOrderHist.
  • templateDirectory - It is possible to override the code generated by SubSonic.  This directory would contain the code templates to override the default templates supplied.  This will be covered in greater detail later when discussing code generation.
  • useSPs - If you do not want a class generated for stored procedures, set this value to false.

Database Connection String

Third, you need to define a database connection string.

<connectionStrings>

<add name="NorthwindConnection" connectionString="Data Source=localhost\SQLExpress; Database=Northwind; Integrated Security=true;"/>

</connectionStrings>

Build Provider Configuration

Fourth, you need to setup a build provider to create the auto generated classes.  This needs to be added to the compilation tag.

<buildProviders>

<add extension=".abp" type="SubSonic.BuildProvider, SubSonic"/>

</buildProviders>

Build Provider Definition

Last, you need to create an .abp file for this build provider to use.  You do this by adding a text file named Builder.abp to the App_Code folder.  Inside this file you indicate which database tables should have auto generate classes.  If you want all tables, just enter *, otherwise, list the tables one per line.

Summary

In summary, these are the items you need to configure.

  • SubSonic Configuration Section
  • Data Provider
  • Database Connection String
  • Build Provider Configuration
  • Build Provider Definition

Here is a sample web.config with all the SubSonic required values defined.  You can also use the web.config included in the sample web site downloaded along with the SubSonic source code as a starting point, which is where these sample values are derived from.

<?xml version="1.0"?>

<configuration>

<configSections>

<section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic" allowDefinition="MachineToApplication" restartOnExternalChanges="true" requirePermission="false"/>

</configSections>

<appSettings/>

<connectionStrings>

<add name="NorthwindConnection" connectionString="Data Source=localhost\SQLExpress; Database=Northwind; Integrated Security=true;"/>

</connectionStrings>

<SubSonicService defaultProvider="SqlDataProvider" spClassName="SPs" fixPluralClassNames="true">

<providers>

<add name="SqlDataProvider" type="SubSonic.SqlDataProvider, SubSonic" connectionStringName="NorthwindConnection"/>

</providers>

</SubSonicService>

<system.web>

<compilation debug="true" defaultLanguage="C#">

<buildProviders>

<add extension=".abp" type="SubSonic.BuildProvider, SubSonic"/>

</buildProviders>

<assemblies>

<add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

<add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>

<add assembly="System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>

<add assembly="MySql.Data, Version=1.0.7.30072, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/></assemblies></compilation>

<authentication mode="Windows"/>

</system.web>

</configuration>

To make sure everything is working, build the web site, then open up the Class View window and you should see class names that match your tables.  For example, using the Northwind database, you should now see classes named Category, Customer and Employee.

Find the entire series of posts here.

Posted on Friday, November 17, 2006 4:57 PM SubSonic | Back to top


Comments on this post: Getting Started with SubSonic - Part 4, Configuration

# Problem with web.config
Requesting Gravatar...
Warning 1 The 'requirePermission' attribute is not declared. D:\WebSite\NetTicket\Web.config 4 148 D:\WebSite\NetTicket\
Left by Fabian on Sep 18, 2007 6:30 AM

# re: Getting Started with SubSonic - Part 4, Configuration
Requesting Gravatar...
very good
Left by Vincent Luo on Mar 11, 2008 8:43 PM

# re: Getting Started with SubSonic - Part 4, Configuration
Requesting Gravatar...
Can you please let me know, how to configure SubSonic with .Net 1.0. I've been using it successfuly with .Net 2.0.

The specific problem i'm facing is that, using .Net 1.0, the <connectionStrings /> tag is not recognized in web.config. And <SubSonicService /> tag requires the parameter connectionStringName as must.

So my question is;

-?- Is there any way to specify connectionString directly to <SubSonicService /> instead of specifying connectionStringName?

Or other wise how to setup web.config settings while using SubSonicService with ASP.Net 1.0

Thanks and regards.
Left by Shakeel on Jun 28, 2008 4:21 AM

# re: Getting Started with SubSonic - Part 4, Configuration
Requesting Gravatar...
it is a good one. this web.cofig file is built successfully but it is not generating class for the tables...how can i solve this problem.please mail me in nafser33@gmail.com
Left by nafser33 on Sep 01, 2008 8:04 AM

# re: Getting Started with SubSonic - Part 4, Configuration
Requesting Gravatar...
Can SubSonic be set up to load dynamically and not depend on the web.config settings? I have a CMS that I would like to use with SubSonic. A single instance of the site hosts multiple websites and some of them have custom data needs. I would like to set up a way to upload a DLL and configure access to it programmatically without having to store settings in the web.config.

Thanks
Left by Bryan Thrasher on Dec 09, 2008 6:36 PM

# re: Getting Started with SubSonic - Part 4, Configuration
Requesting Gravatar...
@Bryan - check out Jim Bonnie as walkspoiled.com...he's got some a DNN example module using SubSonic as the DAL without web.config changes.
Left by Briana Tarrance on Aug 04, 2009 3:03 PM

# Setting up SubSonic and MySQL
Requesting Gravatar...
If anyone needs some info on setting up SubSonic with MySQL you can find a post I wrote on it here:

Setting up SubSonic and MySQL
Left by JeffGos on Nov 16, 2010 10:40 PM

Your comment:
 (will show your gravatar)


Copyright © Scott Kuhl | Powered by: GeeksWithBlogs.net | Join free