Пример #1
0
        /// <summary>
        /// Configure access to the database,
        /// Then issue any pending database migrations to update the database structure to match the model
        /// Then seed the database with sample data to be friendly
        /// </summary>
        /// <param name="host"></param>
        private static void ConfigureDatabase(IWebHost host)
        {
            using (var scope = host.Services.CreateScope()) {
                var services = scope.ServiceProvider;
                var logger   = services.GetRequiredService <ILogger <Program> >();

                try {
                    AssetModelContext context = services.GetRequiredService <AssetModelContext>();
                    bool connected            = false;
                    while (!connected)
                    {
                        try {
                            context = services.GetRequiredService <AssetModelContext>();
                            context.Database.OpenConnection();
                            connected = true;// if no exception throw
                        }
                        catch (Exception) {
                            logger.LogWarning("Failed to open db connection - trying in 5sec ");
                            Thread.Sleep(5000);
                        }
                    }

                    context.Database.Migrate();
                    AssetModelContext.Initialize(services);
                } catch (Exception ex) {
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }
        }
 /// <summary>
 /// Create a new API controller for OVE Asset Models using Dependency Injection
 /// </summary>
 /// <param name="context">Database Context</param>
 /// <param name="logger">logger</param>
 /// <param name="fileOperations">something that understands what to do with files</param>
 /// <param name="serviceRepository">service repository</param>
 public OVEAssetModelController(AssetModelContext context, ILogger <OVEAssetModelController> logger,
                                IAssetFileOperations fileOperations, ServiceRepository serviceRepository)
 {
     _context           = context;
     _logger            = logger;
     _fileOperations    = fileOperations;
     _serviceRepository = serviceRepository;
     _logger.LogInformation("started Asset Controller Started");
 }