Пример #1
0
        public static DbContext Migrate(
            this DbContextOptionsBuilder optionsBuilder,
            bool ensureDeletedThenRecreateIfExists = false,
            bool doubleConfirmIfYouWantToEnsureDeletedThenRecreate = false)
        {
            var contextType = optionsBuilder.Options.ContextType;
            var context     = optionsBuilder.CreateDbContext();

            if (ensureDeletedThenRecreateIfExists && doubleConfirmIfYouWantToEnsureDeletedThenRecreate)
            {
                context.Database.EnsureDeleted();
                _migrated.Remove(contextType);
            }

            if (!_migrated.Contains(contextType))
            {
                lock ( _lock ) {
                    _migrated.Add(contextType);
                }
                context.Database.Migrate();
            }
            return(context);
        }