/// <summary> /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to /// the same compatibility standards as public APIs. It may be changed or removed without notice in /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// </summary> public virtual ContextInfo GetContextInfo([CanBeNull] string contextType) { using var context = CreateContext(contextType); var info = new ContextInfo(); var provider = context.GetService <IDatabaseProvider>(); info.ProviderName = provider.Name; if (((IDatabaseFacadeDependenciesAccessor)context.Database).Dependencies is IRelationalDatabaseFacadeDependencies) { try { var connection = context.Database.GetDbConnection(); info.DataSource = connection.DataSource; info.DatabaseName = connection.Database; } catch (Exception exception) { info.DataSource = info.DatabaseName = DesignStrings.BadConnection(exception.Message); } } else { info.DataSource = info.DatabaseName = DesignStrings.NoRelationalConnection; } var options = context.GetService <IDbContextOptions>(); info.Options = options.BuildOptionsFragment().Trim(); return(info); }
public void GetContextInfo_does_not_throw_if_DbConnection_cannot_be_created() { Exception expected = null; try { new SqlConnection("Cake=None"); } catch (Exception e) { expected = e; } var info = CreateOperations(typeof(TestProgramRelationalBad)).GetContextInfo(nameof(TestContext)); Assert.Equal(DesignStrings.BadConnection(expected.Message), info.DatabaseName); Assert.Equal(DesignStrings.BadConnection(expected.Message), info.DataSource); Assert.Equal("None", info.Options); Assert.Equal("Microsoft.EntityFrameworkCore.SqlServer", info.ProviderName); }