문제

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?

도움이 되었습니까?

해결책 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)

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top