Beispiel #1
0
        /// <summary>
        /// Gets an SQL Local DB instance with the specified name if it exists, otherwise a new instance with the specified name is created.
        /// </summary>
        /// <param name="value">The <see cref="ISqlLocalDbProvider"/> to use to get or create the instance.</param>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance to get or create.</param>
        /// <returns>
        /// An SQL Local DB instance with the name specified by <paramref name="instanceName"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> or <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// <paramref name="value"/> returns <see langword="null"/> when queried for instances.
        /// </exception>
        public static ISqlLocalDbInstance GetOrCreateInstance(this ISqlLocalDbProvider value, string instanceName)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            bool instanceExists = false;

            if (SqlLocalDbApi.IsDefaultInstanceName(instanceName))
            {
                // The default instance is always listed, even if it does not exist,
                // so need to query that separately to verify whether to get or create.
                instanceExists = SqlLocalDbApi.GetInstanceInfo(instanceName).Exists;
            }
            else
            {
                // This approach is used otherwise for testability
                IList <ISqlLocalDbInstanceInfo> instances = value.GetInstances();

                if (instances != null)
                {
                    // Instance names in SQL Local DB are case-insensitive
                    instanceExists = instances
                                     .Where((p) => p != null)
                                     .Where((p) => string.Equals(p.Name, instanceName, StringComparison.OrdinalIgnoreCase))
                                     .Any();
                }
            }

            if (instanceExists)
            {
                return(value.GetInstance(instanceName));
            }
            else
            {
                return(value.CreateInstance(instanceName));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets a SQL Local DB instance with the specified name if it exists, otherwise a new instance with the specified name is created.
        /// </summary>
        /// <param name="api">The <see cref="ISqlLocalDbApi"/> to use to get or create the instance.</param>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance to get or create.</param>
        /// <returns>
        /// A SQL Local DB instance with the name specified by <paramref name="instanceName"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="api"/> or <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        public static ISqlLocalDbInstanceInfo GetOrCreateInstance(this ISqlLocalDbApi api, string instanceName)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            // Instance names in SQL Local DB are case-insensitive
            if (string.Equals(api.DefaultInstanceName, instanceName, StringComparison.OrdinalIgnoreCase) ||
                SqlLocalDbApi.IsDefaultInstanceName(instanceName))
            {
                // The default instance is always listed, even if it does not exist,
                // so need to query that separately to verify whether to get or create.
                ISqlLocalDbInstanceInfo info = api.GetInstanceInfo(instanceName);

                if (info != null)
                {
                    // If it exists (or it's a default instance), we can't create
                    // it so just return the information about it immediately.
                    if (info.Exists || info.IsAutomatic)
                    {
                        return(info);
                    }
                }
            }

            if (api.InstanceExists(instanceName))
            {
                return(api.GetInstanceInfo(instanceName));
            }
            else
            {
                return(api.CreateInstance(instanceName, api.LatestVersion));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new instance of <see cref="ISqlLocalDbInstance"/>.
        /// </summary>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance to create.</param>
        /// <returns>
        /// The created instance of <see cref="ISqlLocalDbInstance"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// The LocalDB instance specified by <paramref name="instanceName"/> already exists or
        /// no LocalDB instance information was returned by the underlying SQL LocalDB API.
        /// </exception>
        /// <exception cref="SqlLocalDbException">
        /// The LocalDB instance specified by <paramref name="instanceName"/> could not be created.
        /// </exception>
        public virtual SqlLocalDbInstance CreateInstance(string instanceName)
        {
            ISqlLocalDbInstanceInfo info = _localDB.GetInstanceInfo(instanceName);

            if (info == null)
            {
                throw new InvalidOperationException(SR.SqlLocalDbProvider_NoInstance);
            }

            if (info.Exists)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbFactory_InstanceExistsFormat,
                    instanceName);

                Logger.Error(Logger.TraceEvent.CreateInstance, message);

                throw new InvalidOperationException(message);
            }

            string version;

            // If creating the default instance, the version number must not be specified
            if (SqlLocalDbApi.IsDefaultInstanceName(instanceName))
            {
                version = string.Empty;
            }
            else
            {
                version = _version ?? _localDB.LatestVersion;
            }

            _localDB.CreateInstance(instanceName, version);

            return(GetInstance(instanceName));
        }