示例#1
0
 private static void TrackDatabase(DatabaseReusability reuseOption, bool exists, string databaseName)
 {
     if (reuseOption == DatabaseReusability.Active && !exists)
     {
         existingDatabases.Add(databaseName);
     }
 }
        public static TContext Build <TContext>(DbContextOptionsBuilder builder,
                                                Func <DbContextOptions, TContext> contextConstructor,
                                                string databaseName,
                                                DatabaseReusability reuseOption = DatabaseReusability.Disabled)
            where TContext : DbContext
        {
            Guard.AgainstNull(nameof(builder), builder);
            Guard.AgainstNull(nameof(contextConstructor), contextConstructor);
            builder.UseInMemoryDatabase(databaseName);
            builder.ReplaceService <IDbContextDependencies, DbContextDependenciesEx>();
            var context = contextConstructor(builder.Options);
            var exists  = existingDatabases.Contains(databaseName);

            if (reuseOption != DatabaseReusability.Disabled && exists)
            {
                return(context);
            }
            TrackDatabase(reuseOption, exists, databaseName);
            context.ResetValueGenerators();
            return(context);
        }