Skip to content

Instantly share code, notes, and snippets.

@kendallmiller
Created January 4, 2014 16:59
Show Gist options
  • Select an option

  • Save kendallmiller/8257393 to your computer and use it in GitHub Desktop.

Select an option

Save kendallmiller/8257393 to your computer and use it in GitHub Desktop.
DbContext constructor override to accept a standard ADO.NET connection string. In this case RamsCore was the name of our EF context.
/// <summary>
/// Create a new context with the specified named connection string from the application configuration
/// </summary>
/// <param name="connectionStringName"></param>
public RamsCore(string connectionStringName)
: base("name=RamsCore")
{
if (string.IsNullOrWhiteSpace(connectionStringName))
throw new ArgumentNullException("connectionStringName", "The name of a standard connection string in the config file must be supplied");
var configurationConnectionString = ConfigurationManager.ConnectionStrings[connectionStringName];
if ((configurationConnectionString == null) || (string.IsNullOrWhiteSpace(configurationConnectionString.ConnectionString)))
throw new InvalidOperationException("The connection string '" + connectionStringName + "' was not found in the configuration file or did not specify a connection string.");
_connectionString = configurationConnectionString.ConnectionString;
Database.Connection.ConnectionString = _connectionString;
LoupeCommandInterceptor.Register();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment