private bool TryInitializeFromAppConfig(string name, AppConfig config) { ConnectionStringSettings connectionInConfig = LazyInternalConnection.FindConnectionInConfig(name, config); if (connectionInConfig == null) { return(false); } this.InitializeFromConnectionStringSetting(connectionInConfig); this._connectionStringOrigin = DbConnectionStringOrigin.Configuration; this._connectionStringName = connectionInConfig.Name; return(true); }
private void Initialize() { if (this.UnderlyingConnection != null) { return; } if (this._connectionInfo != null) { ConnectionStringSettings connectionString = this._connectionInfo.GetConnectionString(this.AppConfig); this.InitializeFromConnectionStringSetting(connectionString); this._connectionStringOrigin = DbConnectionStringOrigin.DbContextInfo; this._connectionStringName = connectionString.Name; } else { string name; if (!DbHelpers.TryGetConnectionName(this._nameOrConnectionString, out name) || !this.TryInitializeFromAppConfig(name, this.AppConfig)) { if (name != null && DbHelpers.TreatAsConnectionString(this._nameOrConnectionString)) { throw Error.DbContext_ConnectionStringNotFound((object)name); } if (DbHelpers.IsFullEFConnectionString(this._nameOrConnectionString)) { this.UnderlyingConnection = (DbConnection) new EntityConnection(this._nameOrConnectionString); } else if (base.ProviderName != null) { this.CreateConnectionFromProviderName(base.ProviderName); } else { this.UnderlyingConnection = DbConfiguration.DependencyResolver.GetService <IDbConnectionFactory>().CreateConnection(name ?? this._nameOrConnectionString); if (this.UnderlyingConnection == null) { throw Error.DbContext_ConnectionFactoryReturnedNullConnection(); } } if (name != null) { this._connectionStringOrigin = DbConnectionStringOrigin.Convention; this._connectionStringName = name; } else { this._connectionStringOrigin = DbConnectionStringOrigin.UserCode; } } } this.OnConnectionInitialized(); }
internal DbContextInfo(DbContext context, Func <IDbDependencyResolver> resolver = null) { Check.NotNull <DbContext>(context, nameof(context)); this._resolver = resolver ?? (Func <IDbDependencyResolver>)(() => DbConfiguration.DependencyResolver); this._contextType = context.GetType(); this._appConfig = AppConfig.DefaultInstance; InternalContext internalContext = context.InternalContext; this._connectionProviderName = internalContext.ProviderName; this._connectionInfo = new DbConnectionInfo(internalContext.OriginalConnectionString, this._connectionProviderName); this._connectionString = internalContext.OriginalConnectionString; this._connectionStringName = internalContext.ConnectionStringName; this._connectionStringOrigin = internalContext.ConnectionStringOrigin; }
/// <summary> /// Called internally when a context info is needed for an existing context, which may not be constructable. /// </summary> /// <param name="context"> The context instance to get info from. </param> internal DbContextInfo(DbContext context) { Check.NotNull(context, "context"); _contextType = context.GetType(); _appConfig = AppConfig.DefaultInstance; var internalContext = context.InternalContext; _connectionProviderName = internalContext.ProviderName; _connectionInfo = new DbConnectionInfo(internalContext.OriginalConnectionString, _connectionProviderName); _connectionString = internalContext.OriginalConnectionString; _connectionStringName = internalContext.ConnectionStringName; _connectionStringOrigin = internalContext.ConnectionStringOrigin; }
// <summary> // Searches the app.config/web.config file for a connection that matches the given name. // The connection might be a store connection or an EF connection. // </summary> // <param name="name"> The connection name. </param> // <returns> True if a connection from the app.config file was found and used. </returns> private bool TryInitializeFromAppConfig(string name, AppConfig config) { DebugCheck.NotNull(config); var appConfigConnection = FindConnectionInConfig(name, config); if (appConfigConnection != null) { InitializeFromConnectionStringSetting(appConfigConnection); _connectionStringOrigin = DbConnectionStringOrigin.Configuration; _connectionStringName = appConfigConnection.Name; return(true); } return(false); }
private DbContextInfo( Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo) { if (!typeof(DbContext).IsAssignableFrom(contextType)) { throw new ArgumentOutOfRangeException("contextType"); } _contextType = contextType; _modelProviderInfo = modelProviderInfo; _appConfig = config; _connectionInfo = connectionInfo; _activator = CreateActivator(); if (_activator != null) { DatabaseInitializerSuppressor.Instance.Suppress(_contextType); var context = _activator(); if (context != null) { context.InternalContext.OnDisposing += (_, __) => DatabaseInitializerSuppressor.Instance.Unsuppress(_contextType); _isConstructible = true; PushConfiguration(context); using (context) { ConfigureContext(context); _connectionString = context.InternalContext.Connection.ConnectionString; _connectionStringName = context.InternalContext.ConnectionStringName; _connectionProviderName = context.InternalContext.ProviderName; _connectionStringOrigin = context.InternalContext.ConnectionStringOrigin; } } } }
// <summary> // Called internally when a context info is needed for an existing context, which may not be constructable. // </summary> // <param name="context"> The context instance to get info from. </param> internal DbContextInfo(DbContext context, Func <IDbDependencyResolver> resolver = null) { Check.NotNull(context, "context"); _resolver = resolver ?? (() => DbConfiguration.DependencyResolver); _contextType = context.GetType(); _appConfig = AppConfig.DefaultInstance; var internalContext = context.InternalContext; _connectionProviderName = internalContext.ProviderName; _connectionInfo = new DbConnectionInfo(internalContext.OriginalConnectionString, _connectionProviderName); _connectionString = internalContext.OriginalConnectionString; _connectionStringName = internalContext.ConnectionStringName; _connectionStringOrigin = internalContext.ConnectionStringOrigin; }
private DbContextInfo( Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func <IDbDependencyResolver> resolver = null) { if (!typeof(DbContext).IsAssignableFrom(contextType)) { throw new ArgumentOutOfRangeException("contextType"); } _resolver = resolver ?? (() => DbConfiguration.DependencyResolver); _contextType = contextType; _modelProviderInfo = modelProviderInfo; _appConfig = config; _connectionInfo = connectionInfo; _activator = CreateActivator(); if (_activator != null) { var context = CreateInstance(); if (context != null) { _isConstructible = true; using (context) { _connectionString = DbInterception.Dispatch.Connection.GetConnectionString( context.InternalContext.Connection, new DbInterceptionContext().WithDbContext(context)); _connectionStringName = context.InternalContext.ConnectionStringName; _connectionProviderName = context.InternalContext.ProviderName; _connectionStringOrigin = context.InternalContext.ConnectionStringOrigin; } } } }
private DbContextInfo( Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo) { if (!typeof(DbContext).IsAssignableFrom(contextType)) { throw new ArgumentOutOfRangeException("contextType"); } _contextType = contextType; _modelProviderInfo = modelProviderInfo; _appConfig = config; _connectionInfo = connectionInfo; _activator = CreateActivator(); if (_activator != null) { var context = _activator(); if (context != null) { _isConstructible = true; PushConfiguration(context); using (context) { ConfigureContext(context); _connectionString = context.InternalContext.Connection.ConnectionString; _connectionStringName = context.InternalContext.ConnectionStringName; _connectionProviderName = context.InternalContext.ProviderName; _connectionStringOrigin = context.InternalContext.ConnectionStringOrigin; } } } }
private DbContextInfo( Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func <IDbDependencyResolver> resolver = null) { if (!typeof(DbContext).IsAssignableFrom(contextType)) { throw new ArgumentOutOfRangeException(nameof(contextType)); } this._resolver = resolver ?? (Func <IDbDependencyResolver>)(() => DbConfiguration.DependencyResolver); this._contextType = contextType; this._modelProviderInfo = modelProviderInfo; this._appConfig = config; this._connectionInfo = connectionInfo; this._activator = this.CreateActivator(); if (this._activator == null) { return; } DbContext instance = this.CreateInstance(); if (instance == null) { return; } this._isConstructible = true; using (instance) { this._connectionString = DbInterception.Dispatch.Connection.GetConnectionString(instance.InternalContext.Connection, new DbInterceptionContext().WithDbContext(instance)); this._connectionStringName = instance.InternalContext.ConnectionStringName; this._connectionProviderName = instance.InternalContext.ProviderName; this._connectionStringOrigin = instance.InternalContext.ConnectionStringOrigin; } }
/// <summary> /// Called internally when a context info is needed for an existing context, which may not be constructable. /// </summary> /// <param name="context">The context instance to get info from.</param> internal DbContextInfo(DbContext context) { //Contract.Requires(context != null); _contextType = context.GetType(); _appConfig = AppConfig.DefaultInstance; var internalContext = context.InternalContext; _connectionProviderName = internalContext.ProviderName; _connectionInfo = new DbConnectionInfo(internalContext.OriginalConnectionString, _connectionProviderName); _connectionString = internalContext.OriginalConnectionString; _connectionStringName = internalContext.ConnectionStringName; _connectionStringOrigin = internalContext.ConnectionStringOrigin; }
// <summary> // Creates the underlying <see cref="DbConnection" /> (which may actually be an <see cref="EntityConnection" />) // if it does not already exist. // </summary> private void Initialize() { if (UnderlyingConnection == null) { Debug.Assert(AppConfig != null); string name; if (_connectionInfo != null) { var connection = _connectionInfo.GetConnectionString(AppConfig); InitializeFromConnectionStringSetting(connection); _connectionStringOrigin = DbConnectionStringOrigin.DbContextInfo; _connectionStringName = connection.Name; } // If the name or connection string is a simple name or is in the form "name=xyz" then use // that name to try to load from the app/web config file. else if (!DbHelpers.TryGetConnectionName(_nameOrConnectionString, out name) || !TryInitializeFromAppConfig(name, AppConfig)) { // If the connection string is of the form name=, but the name was not found in the config file // then always throw since we always interpret name= to mean find in the config file only. if (name != null && DbHelpers.TreatAsConnectionString(_nameOrConnectionString)) { throw Error.DbContext_ConnectionStringNotFound(name); } // If the name or connection string is a full EF connection string, then create an EntityConnection from it. if (DbHelpers.IsFullEFConnectionString(_nameOrConnectionString)) { UnderlyingConnection = new EntityConnection(_nameOrConnectionString); } else { if (base.ProviderName != null) { CreateConnectionFromProviderName(base.ProviderName); } else { // Otherwise figure out the connection factory to use (either the default, // the one set in code, or one provided by DbContextInfo via the AppSettings property UnderlyingConnection = DbConfiguration.DependencyResolver.GetService <IDbConnectionFactory>() .CreateConnection(name ?? _nameOrConnectionString); if (UnderlyingConnection == null) { throw Error.DbContext_ConnectionFactoryReturnedNullConnection(); } } } if (name != null) { _connectionStringOrigin = DbConnectionStringOrigin.Convention; _connectionStringName = name; } else { _connectionStringOrigin = DbConnectionStringOrigin.UserCode; } } OnConnectionInitialized(); } Debug.Assert(UnderlyingConnection != null, "Connection should have been initialized by some mechanism."); }
private bool TryInitializeFromAppConfig(string name, AppConfig config) { DebugCheck.NotNull(config); var appConfigConnection = FindConnectionInConfig(name, config); if (appConfigConnection != null) { InitializeFromConnectionStringSetting(appConfigConnection); _connectionStringOrigin = DbConnectionStringOrigin.Configuration; _connectionStringName = appConfigConnection.Name; return true; } return false; }
/// <summary> /// Creates the underlying <see cref="DbConnection" /> (which may actually be an <see cref="EntityConnection" />) /// if it does not already exist. /// </summary> private void Initialize() { if (UnderlyingConnection == null) { Debug.Assert(AppConfig != null); string name; if (_connectionInfo != null) { var connection = _connectionInfo.GetConnectionString(AppConfig); InitializeFromConnectionStringSetting(connection); _connectionStringOrigin = DbConnectionStringOrigin.DbContextInfo; _connectionStringName = connection.Name; } // If the name or connection string is a simple name or is in the form "name=foo" then use // that name to try to load from the app/web config file. else if (!DbHelpers.TryGetConnectionName(_nameOrConnectionString, out name) || !TryInitializeFromAppConfig(name, AppConfig)) { // If the connection string is of the form name=, but the name was not found in the config file // then always throw since we always interpret name= to mean find in the config file only. if (name != null && DbHelpers.TreatAsConnectionString(_nameOrConnectionString)) { throw Error.DbContext_ConnectionStringNotFound(name); } // If the name or connection string is a full EF connection string, then create an EntityConnection from it. if (DbHelpers.IsFullEFConnectionString(_nameOrConnectionString)) { UnderlyingConnection = new EntityConnection(_nameOrConnectionString); } else { if (base.ProviderName != null) { CreateConnectionFromProviderName(base.ProviderName); } else { // Otherwise figure out the connection factory to use (either the default, // the one set in code, or one provided by DbContextInfo via the AppSettings property UnderlyingConnection = DbConfiguration.DependencyResolver.GetService<IDbConnectionFactory>() .CreateConnection(name ?? _nameOrConnectionString); if (UnderlyingConnection == null) { throw Error.DbContext_ConnectionFactoryReturnedNullConnection(); } } } if (name != null) { _connectionStringOrigin = DbConnectionStringOrigin.Convention; _connectionStringName = name; } else { _connectionStringOrigin = DbConnectionStringOrigin.UserCode; } } OnConnectionInitialized(); } Debug.Assert(UnderlyingConnection != null, "Connection should have been initialized by some mechanism."); }
private DbContextInfo( Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func<IDbDependencyResolver> resolver = null) { if (!typeof(DbContext).IsAssignableFrom(contextType)) { throw new ArgumentOutOfRangeException("contextType"); } _resolver = resolver ?? (() => DbConfiguration.DependencyResolver); _contextType = contextType; _modelProviderInfo = modelProviderInfo; _appConfig = config; _connectionInfo = connectionInfo; _activator = CreateActivator(); if (_activator != null) { var context = CreateInstance(); if (context != null) { _isConstructible = true; using (context) { _connectionString = DbInterception.Dispatch.Connection.GetConnectionString( context.InternalContext.Connection, new DbInterceptionContext().WithDbContext(context)); _connectionStringName = context.InternalContext.ConnectionStringName; _connectionProviderName = context.InternalContext.ProviderName; _connectionStringOrigin = context.InternalContext.ConnectionStringOrigin; } } } }
// <summary> // Called internally when a context info is needed for an existing context, which may not be constructable. // </summary> // <param name="context"> The context instance to get info from. </param> internal DbContextInfo(DbContext context, Func<IDbDependencyResolver> resolver = null) { Check.NotNull(context, "context"); _resolver = resolver ?? (() => DbConfiguration.DependencyResolver); _contextType = context.GetType(); _appConfig = AppConfig.DefaultInstance; var internalContext = context.InternalContext; _connectionProviderName = internalContext.ProviderName; _connectionInfo = new DbConnectionInfo(internalContext.OriginalConnectionString, _connectionProviderName); _connectionString = internalContext.OriginalConnectionString; _connectionStringName = internalContext.ConnectionStringName; _connectionStringOrigin = internalContext.ConnectionStringOrigin; }