public void CheckIfBuiltTypeHasValidParent() { DbContextBuilder contextBuilder = new DbContextBuilder(); Type dbContextType = contextBuilder.Build(); Assert.Equal(typeof(ConfigurableDbContext), dbContextType.BaseType); }
public void CheckIfBuiltTypeIsClass() { DbContextBuilder contextBuilder = new DbContextBuilder(); Type dbContextType = contextBuilder.Build(); Assert.True(dbContextType.IsClass); }
public static async Task InitializeAsync <T>( IConfigurationRoot config, string migrationPath, string connectionKey = AzureSqlConstants.AzureSqlConnectionKey) where T : DbContext { Console.WriteLine("Start migration..."); try { using (T context = DbContextBuilder.Build <T>(config, migrationPath, connectionKey)) { await context.Database.MigrateAsync(); await context.Database.ExecuteSqlRawAsync(File.ReadAllText(AzureSqlConstants.PrivateChatView)); await context.Database.EnsureCreatedAsync(); } Console.WriteLine("End migration..."); } catch (Exception ex) { Console.WriteLine($"An error occurred: {ex.Message}"); throw; } }
public OnlineChatServiceDbContext CreateDbContext(string[] args) { IConfigurationRoot config = ConfigurationBuilderHelper.Create(Directory.GetCurrentDirectory()); return(DbContextBuilder.Build <OnlineChatServiceDbContext>( config, "OCS.DAL.EF.Migrations", AzureSqlConstants.AzureSqlConnectionKey)); }
public PaymentDbContext CreateDbContext(string[] args) { IConfigurationRoot config = Shared.Utils.Initialization.ConfigurationBuilder .Create(Directory.GetCurrentDirectory()); return(DbContextBuilder.Build <PaymentDbContext>( config, "GSP.Payment.Data.Migrations", SettingKeyConstants.AzureSqlConnectionKey)); }
public void CheckIfBuiltTypeHasValidDbSet() { Type testClassType = typeof(ClassForDbContextBuilderTest); DbContextBuilder contextBuilder = new DbContextBuilder() .WithEntity(testClassType); Type dbContextType = contextBuilder.Build(); Type expectedDbSetType = typeof(DbSet <>) .MakeGenericType(testClassType); PropertyInfo dbSetProperty = dbContextType.GetProperties() .SingleOrDefault(p => p.PropertyType == expectedDbSetType); Assert.NotNull(dbSetProperty); }
/// <summary> /// Extensions method that enables registering DbContext based on provided builder /// </summary> /// <param name="services"></param> /// <param name="contextBuilder">DbContext subtype builder that cannot be null</param> public static void AddDbContext(this IServiceCollection services, DbContextBuilder contextBuilder) { if (contextBuilder == null) { throw new ArgumentNullException("Context builder cannot be null"); } IDbContextOptionsCreator optionsCreator = contextBuilder.OptionsCreator ?? new DelegatedDbContextOptionsCreator(_ => {}); IDbContextModelCreator modelCreator = contextBuilder.ModelCreator ?? new DelegatedDbContextModelCreator(_ => {}); Type dbContextType = contextBuilder.Build(); services.AddSingleton(typeof(IDbContextOptionsCreator), optionsCreator); services.AddSingleton(typeof(IDbContextModelCreator), modelCreator); services.AddScoped(typeof(DbContext), dbContextType); }