예제 #1
0
        private static void AddTodoContext(IServiceProvider serviceProvider, DbContextOptionsBuilder options)
        {
            // Check that SQL Server LocalDB is installed
            ISqlLocalDbApi localDB = serviceProvider.GetRequiredService <ISqlLocalDbApi>();

            if (!localDB.IsLocalDBInstalled())
            {
                throw new NotSupportedException("SQL LocalDB is not installed.");
            }

            // Get the configured SQL LocalDB instance to store the TODO items in, creating it if it does not exist
            IConfiguration          config   = serviceProvider.GetRequiredService <IConfiguration>();
            ISqlLocalDbInstanceInfo instance = localDB.GetOrCreateInstance(config["SqlLocalDbInstance"]);

            // Ensure that the SQL LocalDB instance is running and start it if not already running
            if (!instance.IsRunning)
            {
                instance.Manage().Start();
            }

            // Get the SQL connection string to use to connect to the LocalDB instance
            string connectionString = instance.GetConnectionString();

            options.UseSqlServer(connectionString);
        }
예제 #2
0
        public void GetOrCreateInstance_Throws_If_InstanceName_Is_Null()
        {
            // Arrange
            ISqlLocalDbApi api          = Mock.Of <ISqlLocalDbApi>();
            string         instanceName = null;

            // Act and Assert
            Assert.Throws <ArgumentNullException>("instanceName", () => api.GetOrCreateInstance(instanceName));
        }
예제 #3
0
        public void GetOrCreateInstance_Throws_If_Api_Is_Null()
        {
            // Arrange
            ISqlLocalDbApi api          = null;
            string         instanceName = "SomeName";

            // Act and Assert
            Assert.Throws <ArgumentNullException>("api", () => api.GetOrCreateInstance(instanceName));
        }
예제 #4
0
        /// <summary>
        /// Creates a new local instance, either by finding an existing instance, or generating a temporary instance.
        /// </summary>
        /// <param name="api"></param>
        /// <param name="instanceName"></param>
        /// <returns></returns>
        ISqlLocalDbInstanceInfo CreateLocalDbInstance(ISqlLocalDbApi api, string instanceName)
        {
            if (api.GetOrCreateInstance(instanceName) is ISqlLocalDbInstanceInfo info && info.Exists)
            {
                return(info);
            }

            throw new InvalidOperationException("Unable to create instance.");
        }
예제 #5
0
        /// <summary>
        /// Gets the default SQL Local DB instance.
        /// </summary>
        /// <param name="api">The <see cref="ISqlLocalDbApi"/> to use to get the default instance.</param>
        /// <returns>
        /// The default SQL Local DB instance.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="api"/> is <see langword="null"/>.
        /// </exception>
        public static ISqlLocalDbInstanceInfo GetDefaultInstance(this ISqlLocalDbApi api)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            return(api.GetOrCreateInstance(api.DefaultInstanceName));
        }
        public static IServiceCollection AddSqlLocalDbInstance(this IServiceCollection services, Action <TOptions> setupAction = null)
        {
            if (setupAction != null)
            {
                services.Configure(setupAction);
            }

            services.AddSingleton <ISqlLocalDbInstanceInfo>(provider =>
            {
                var options = provider.GetService <IOptions <TOptions> >() ?? new OptionsWrapper <TOptions>(provider.GetRequiredService <TOptions>());

                ISqlLocalDbApi localDB = provider.GetRequiredService <ISqlLocalDbApi>();

                if (!localDB.IsLocalDBInstalled())
                {
                    throw new NotSupportedException("SQL LocalDB is not installed.");
                }

                // Get the configured SQL LocalDB instance to store the TODO items in, creating it if it does not exist

                ISqlLocalDbInstanceInfo instance = localDB.GetOrCreateInstance(options?.Value?.InstanceName ?? "SqlLocalDb-DefaultDb");

                // Ensure that the SQL LocalDB instance is running and start it if not already running
                if (!instance.IsRunning)
                {
                    instance.Manage().Start();
                }

                // Get the SQL connection string to use to connect to the LocalDB instance
                //string connectionString = instance.GetConnectionString();
                return(instance);
            });

            services.AddSingleton <Server>(provider =>
            {
                var options = provider.GetService <IOptions <TOptions> >() ?? new OptionsWrapper <TOptions>(provider.GetRequiredService <TOptions>());

                var instance      = provider.GetRequiredService <ISqlLocalDbInstanceInfo>();
                string connString = instance.GetConnectionString();
                //var conn = new SqlConnection(connString);
                var builder            = instance.CreateConnectionStringBuilder();
                builder.InitialCatalog = options?.Value.DatabaseName ?? "";
                var conn = new SqlConnection(builder.ConnectionString);

                var server = new Server(new ServerConnection(conn));
                return(server);
            });


            return(services);
        }
        public void GetOrCreateInstance_Returns_The_Default_Instance_If_It_Exists(string instanceName)
        {
            // Arrange
            var mock = new Mock <ISqlLocalDbApi>();

            mock.Setup((p) => p.DefaultInstanceName)
            .Returns("Blah");

            mock.Setup((p) => p.GetInstanceInfo(instanceName))
            .Returns(CreateInstanceInfo(exists: true));

            ISqlLocalDbApi api = mock.Object;

            // Act
            ISqlLocalDbInstanceInfo actual = api.GetOrCreateInstance(instanceName);

            // Assert
            actual.ShouldNotBeNull();
            actual.Exists.ShouldBeTrue();
        }
        public void GetOrCreateInstance_Returns_An_Instance_If_It_Does_Not_Exist()
        {
            // Arrange
            string instanceName = "MyInstance";

            var mock = new Mock <ISqlLocalDbApi>();

            mock.Setup((p) => p.DefaultInstanceName)
            .Returns("Blah");

            mock.Setup((p) => p.LatestVersion)
            .Returns("v99.0");

            mock.Setup((p) => p.CreateInstance(instanceName, "v99.0"))
            .Returns(CreateInstanceInfo(exists: false));

            ISqlLocalDbApi api = mock.Object;

            // Act
            ISqlLocalDbInstanceInfo actual = api.GetOrCreateInstance(instanceName);

            // Assert
            actual.ShouldNotBeNull();
        }