/// <summary>
        /// Verifies the database connection.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Returns an awaitable <see cref="Task"/>.</returns>
        private async Task VerifyDatabaseConnection(CancellationToken cancellationToken)
        {
            try
            {
                Console.WriteLine("Starting database verification..");
                NeuralmDbContext neuralmDbContext = _serviceProvider.GetService <IFactory <NeuralmDbContext> >().Create();
                if (neuralmDbContext.Database.IsInMemory())
                {
                    Console.WriteLine("InMemory database provider found.");
                    neuralmDbContext.Database.EnsureCreated();
                    Console.WriteLine("Ensured that the database is created.");
                    return;
                }

                RelationalDatabaseCreator relationalDatabaseCreator = neuralmDbContext.Database.GetService <IDatabaseCreator>() as RelationalDatabaseCreator;
                if (relationalDatabaseCreator is null)
                {
                    throw new TargetException("The DbProvider is not relational database provider.");
                }

                bool exists = relationalDatabaseCreator.Exists();
                cancellationToken.ThrowIfCancellationRequested();
                Console.WriteLine($"Database {(exists ? "" : "does not ")}exists.");
                if (!exists)
                {
                    relationalDatabaseCreator.Create();
                    Console.WriteLine("Database created.");
                }

                IEnumerable <string> pendingMigrations = neuralmDbContext.Database.GetPendingMigrations().ToList();
                bool hasPendingMigrations = pendingMigrations.Any();
                Console.WriteLine($"Database {(hasPendingMigrations ? "has" : "does not have")} pending migrations.");
                if (hasPendingMigrations)
                {
                    Console.WriteLine("Would you like to clear the database using EnsureDeleted. And, afterwards reapply all migrations? y/n");
                    ConsoleKeyInfo keyInfo = await WaitForReadKey(cancellationToken);

                    cancellationToken.ThrowIfCancellationRequested();
                    if (keyInfo.KeyChar == 'y')
                    {
                        neuralmDbContext.Database.EnsureDeleted();
                        cancellationToken.ThrowIfCancellationRequested();
                        foreach (string migration in neuralmDbContext.Database.GetMigrations())
                        {
                            Console.WriteLine($"\tMigration: {migration}");
                        }
                    }
                    else
                    {
                        foreach (string pendingMigration in pendingMigrations)
                        {
                            Console.WriteLine($"\tPending migration: {pendingMigration}");
                        }
                    }

                    neuralmDbContext.Database.Migrate();
                    cancellationToken.ThrowIfCancellationRequested();
                    Console.WriteLine("Applied the migrations to the database.");
                }

                Console.WriteLine("Finished database verification!");
            }
            catch (Exception e)
            {
                if (!cancellationToken.IsCancellationRequested)
                {
                    Console.WriteLine($"{nameof(VerifyDatabaseConnection)}: {e.Message}");
                    _cancellationTokenSource.Cancel();
                }
                Console.WriteLine("VerifyDatabaseConnection is cancelled.");
            }
        }
Beispiel #2
0
 /// <summary>
 /// Initializes an instance of the <see cref="NeuralmDbContextRepository{TEntity}"/> class.
 /// </summary>
 /// <param name="dbContext">The database context.</param>
 /// <param name="entityValidator">The entity validator.</param>
 public NeuralmDbContextRepository(NeuralmDbContext dbContext, IEntityValidator <TEntity> entityValidator) : base(dbContext, entityValidator)
 {
 }