Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CatalogCommand" /> class.
        /// </summary>
        /// <param name="action">The action running with this command.</param>
        /// <param name="commandSettings">The command settings associated with this command.</param>
        /// <param name="loggerFactory">The logger factory.</param>
        /// <param name="configuration">The configuration service</param>
        /// <exception cref="ArgumentNullException">loggerFactory</exception>
        public CatalogCommand(
            IAction action,
            Settings.Command commandSettings,
            ILoggerFactory loggerFactory,
            IConfiguration configuration)
            : base(action, commandSettings, loggerFactory)
        {
            if (loggerFactory == null)
            {
                throw new ArgumentNullException(nameof(loggerFactory));
            }

            Logger = loggerFactory.CreateLogger <CatalogCommand>();

            var connectionStringName = commandSettings.Settings.ConnectionStringName();

            if (connectionStringName == null)
            {
                connectionStringName = "PackageModel";
                Logger.LogInformation("No connection string name configured for Catalog command. Using \"PackageModel\".");
            }

            var connectionString = configuration.GetConnectionString(connectionStringName);

            if (string.IsNullOrEmpty(connectionString))
            {
                throw new InvalidOperationException($"No connection string found with the name \"{commandSettings.Settings.ConnectionStringName()}\" ");
            }

            dbContext = new PackageModelContext(connectionString);

            if (!string.IsNullOrEmpty(commandSettings.Settings.NormalizeRegEx()))
            {
                normalizeNameRegex = new Regex(commandSettings.Settings.NormalizeRegEx());
            }

            if (action.ActionSettings.IncludePrerelease)
            {
                Logger.LogWarning($"The {nameof(CatalogCommand)} does not support pre-release versions.");
            }
        }
        /// <summary>An extension method that reverts the changes associated with a PackageModelContext Entity Framework context.</summary>
        /// <param name="context">The Entity Framework context to revert.</param>
        /// <param name="exception">The exception causing the revert.</param>
        public static void RevertChanges(this PackageModelContext context, DbEntityValidationException exception)
        {
            foreach (DbEntityValidationResult item in exception.EntityValidationErrors)
            {
                DbEntityEntry entry = item.Entry;
                switch (entry.State)
                {
                case EntityState.Added:
                    entry.State = EntityState.Detached;
                    break;

                case EntityState.Modified:
                    entry.CurrentValues.SetValues(entry.OriginalValues);
                    entry.State = EntityState.Unchanged;
                    break;

                case EntityState.Deleted:
                    entry.State = EntityState.Unchanged;
                    break;
                }
            }
        }