Entity Framework 6 – Get edmx connection string

/// <summary>
/// Extensions for Entity Framework DbContext
/// </summary>
public static class DbContextExtensions
{
 /// <summary>
 /// Gets the edmx connection string from an active DbContext.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <returns></returns>
 public static string GetEdmxConnectionString(this DbContext context)
 {
 return ((EntityConnection)ConvertContext(context).Connection).ConnectionString;
 }
 /// <summary>
 /// Converts the DbContext to an ObjectContext.
 /// </summary>
 /// <param name="db">The DbContext to convert.</param>
 /// <returns></returns>
 public static ObjectContext ConvertContext(this DbContext db)
 {
 return ((IObjectContextAdapter)db).ObjectContext;
 }
}

Leave a Reply

Your email address will not be published. Required fields are marked *