问题
I'm using SubSonic 2 from within a project and I'd like to specify a different .config file from the default App.config. How can I tell SubSonic to use a specific config file?
回答1:
You can't - SubSonic works from Provider settings that are set for the executing environment. You could, if you wanted, use a connectionStrings.config and put that somewhere else, but SubSonic uses ConfigurationManager to open up the app's config and find it's goodies.
回答2:
It appears that you can do this by setting two properties of SubSonic objects: DataService.ConfigSection
and DataProvider.DefaultConnectionString
. Once those are set SubSonic won't try to look for the default application configuration file, it'll just use the details you've given it.
For example:
// Part 1: set the config section:
string configPath = Assembly.GetExecutingAssembly().Location + ".config";
ExeConfigurationFileMap execfg = new ExeConfigurationFileMap();
execfg.ExeConfigFilename = configPath;
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(execfg, ConfigurationUserLevel.None);
DataService.ConfigSection = (SubSonicSection)configuration.GetSection(ConfigurationSectionName.SUB_SONIC_SERVICE);
// Part 2: set the connection string
string connectionString = configuration.ConnectionStrings.ConnectionStrings["xLocal"].ConnectionString;
DataProvider provider = DataService.Provider;
provider.DefaultConnectionString = connectionString;
This appears to work well, however I am sometimes experiencing a long delay on the 2nd to last line DataProvider provider = DataService.Provider;
. I'm not sure if this is to do with what I'm doing here or it's a general assembly-loading problem. I've documented this problem in another question here: Call to System.Web.Configuration.ProvidersHelper.InstantiateProviders taking ages and ages (from SubSonic)
来源:https://stackoverflow.com/questions/2079781/how-can-i-tell-subsonic-2-to-use-a-different-config-file