Skip to content

Instantly share code, notes, and snippets.

@srn
Created June 1, 2011 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srn/1003401 to your computer and use it in GitHub Desktop.
Save srn/1003401 to your computer and use it in GitHub Desktop.
public class OpenTrackerContext
{
public static string ConnectionString = string.Empty;
private const string APP_CONFIG_KEY = "CONNECTIONSTRING";
private const string EDMX_PATH = "OpenTrackerEntities";
static OpenTrackerContext()
{
if (string.IsNullOrEmpty(ConfigurationManager.AppSettings[APP_CONFIG_KEY]))
throw new ConfigurationErrorsException(APP_CONFIG_KEY + " was not found in Application Settings");
var conn = ConfigurationManager.AppSettings[APP_CONFIG_KEY];
if (ConfigurationManager.ConnectionStrings[conn] == null)
throw new ConfigurationErrorsException(conn + " was not found in Connection Strings");
var _connectionString = ConfigurationManager.ConnectionStrings[conn].ConnectionString;
if (string.IsNullOrEmpty(_connectionString))
throw new ConfigurationErrorsException(conn + " cannot have an empty connection string");
const string META_DATA = @"metadata=res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl;provider=MySql.Data.MySqlClient;provider connection string=""{1};Persist Security Info=True""";
ConnectionString = string.Format(META_DATA, EDMX_PATH, _connectionString);
}
public OpenTrackerDbContext New()
{
var context = new OpenTrackerDbContext(ConnectionString);
return context;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment