I have an Event System which is using Entity Framework. I now get the error that the connection string with name 'X' cannot be found in the application config file.
In which config file do I have to place the connectionstring?
Tridion Stack Exchange is a question and answer site for Tridion developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have an Event System which is using Entity Framework. I now get the error that the connection string with name 'X' cannot be found in the application config file.
In which config file do I have to place the connectionstring?
You can read data from app config with something like this:
private static string GetFromAppSettings(string fieldName)
{
Configuration myDllConfig = ConfigurationManager.OpenExeConfiguration(typeof(PublishLogging).Assembly.Location);
AppSettingsSection myDllConfigAppSettings = (AppSettingsSection)myDllConfig.GetSection("appSettings");
String value = myDllConfigAppSettings.Settings[fieldName].Value;
if (value != null)
{
return value;
}
else
{
return String.Empty;
}
}
Also, please make note that config file should be located in the same folder in which your event system dll is. That being said, please make note also that it should have same name as your built dll with .config
extension.
If you still have problems, I suggest you remote debug event system. You can use this article for reference how:
/// <summary>
/// Returns the Configuration object next to the current executing DLL
/// </summary>
private static Configuration _dllConfiguration = null;
private static Configuration DllConfiguration
{
get
{
if (_dllConfiguration == null)
{
try
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = Assembly.GetExecutingAssembly().Location + ".config"
};
_dllConfiguration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
}
catch (Exception ex)
{
// do nothing
}
}
return _dllConfiguration;
}
}
Then when using it,
string connectionString = DllConfiguration.ConnectionStrings.ConnectionStrings["MyConnection"];
Your app.config (or YourEvent.dll.config) will be like this
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="Data Source=server,port;Initial Catalog=CustomDB;Persist Security Info=True;User ID=user;Password=xxx"/>
</connectionStrings>
</configuration>
The solution was a combination of the above. I already used the '.dll.config' file for configuration (via a custom class ConfigurationHelper). But .NET wasn't able to recognize the connectionstrings in that file.
I solved the issue by setting the connectionstring as appsetting:
<appSettings>
<add key="configConnectionString" value="myconnectionString" />
</appSettings>
And then create a DbContext class as follows:
public MyEntities(string configConnectionString) : base(ConfigurationHelper.GetAppSetting(configConnectionString))
{
}
So you can create the context like this:
MyEntities context = new MyEntities("configConnectionString");