示例#1
0
        public static void Main(string[] args)
        {
            IHost            host             = CreateHostBuilder(args).Build();
            DatabaseDeployer databaseDeployer = (DatabaseDeployer)host.Services.GetService(typeof(DatabaseDeployer));

            databaseDeployer.DeployAsync().Wait();
            host.Run();
        }
        private void DeployDatabase(IConfigurationRoot configuration)
        {
            _connectionString = configuration.GetConnectionString("DefaultConnection");
            _databaseDeployer = new DatabaseDeployer(_connectionString, "Sql\\Schema.sql", "Sql\\Procedures.sql");
            bool isDBCreated = _databaseDeployer.ExecuteReader("IF (OBJECT_ID ('dbo.GetBalance', 'P') IS NULL) SELECT 0 ELSE SELECT 1;", IsDatabaseCreated);

            if (!isDBCreated)
            {
                _databaseDeployer.DeployAsync().Wait();
            }
        }
        private static IServiceProvider GetServiceProvider()
        {
            IConfigurationRoot config = new ConfigurationBuilder()
                                        .AddJsonFile("appsettings.json")
                                        .Build();
            string connectionStr = config.GetConnectionString("DefaultConnection");

            DatabaseDeployer databaseDeployer = new DatabaseDeployer(connectionStr, "Sql\\Schema.sql", "Sql\\Procedures.sql", "Sql\\Initialization.sql");

            databaseDeployer.DeployAsync().Wait();
            SchoolManagerDb schoolManagerDb = new SchoolManagerDb(connectionStr);
            StudentService  studentService  = new StudentService(schoolManagerDb);

            ServiceCollection services = new ServiceCollection();

            services.AddSingleton <ISchoolManagerDb>(x => schoolManagerDb);
            services.AddSingleton <IStudentService>(x => studentService);

            return(services.BuildServiceProvider());
        }