Exemple #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);
        }
        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);
        }