Code:using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage; /// <summary> /// Provides helper methods for working with <see cref="DbContext"/> instances in Entity Framework Core. /// </summary> /// <remarks> /// This static class includes extension methods to check the existence of a database and determine /// whether the database contains any tables. It relies on services provided by Entity Framework Core /// to perform these operations. /// </remarks> internal static class DbContextHelpers { /// <summary> /// Checks if the database associated with the provided <see cref="DbContext"/> exists. /// </summary> /// <param name="context">The database context to check for the existence of the database.</param> /// <returns> /// <c>true</c> if the database exists; otherwise, <c>false</c>. /// </returns> /// <exception cref="InvalidOperationException"> /// Thrown when the database creator service is not available in the provided context. /// </exception> public static bool DatabaseExists<TContext>(this TContext context) where TContext : DbContext => context.GetService<IDatabaseCreator>() is not RelationalDatabaseCreator databaseCreator ? throw new InvalidOperationException("Database creator service is not available.") : databaseCreator.Exists(); /// <summary> /// Determines whether the database associated with the provided <see cref="DbContext"/> contains any tables. /// </summary> /// <typeparam name="TContext">The type of the database context, which must derive from <see cref="DbContext"/>.</typeparam> /// <param name="context">The database context to check for the presence of tables.</param> /// <returns> /// <c>true</c> if the database contains one or more tables; otherwise, <c>false</c>. /// </returns> /// <exception cref="InvalidOperationException"> /// Thrown when the relational database creator service is not available in the provided context. /// </exception> public static bool HasTables<TContext>(this TContext context) where TContext : DbContext => context.GetService<IRelationalDatabaseCreator>() is not RelationalDatabaseCreator databaseCreator ? throw new InvalidOperationException("Database creator service is not available.") : databaseCreator.HasTables(); }


Reply With Quote