Ejemplo n.º 1
0
        public static async Task SeedAsync(ConfigDbContext db,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            db.Database.EnsureCreated();

            int retryForAvailability = retry.Value;

            try
            {
                // TODO: Only run this if using a real database
                // context.Database.Migrate();

                if (!db.ConfigurationValues.Any())
                {
                    db.ConfigurationValues.AddRange(
                        GetPreconfiguredCatalogBrands());

                    await db.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <ConfigDbContextSeed>();
                    log.LogError(ex.Message);
                    await SeedAsync(db, loggerFactory, retryForAvailability);
                }
            }
        }
Ejemplo n.º 2
0
        private IDictionary <string, string> InitData(ConfigDbContext dbContext)
        {
            Data = Source.InitialData;

            InsertOrUpdateData(dbContext);

            return(Data);
        }
Ejemplo n.º 3
0
        private void InsertOrUpdateData(ConfigDbContext dbContext)
        {
            var values = dbContext.ConfigurationValues.ToArray();

            dbContext.ConfigurationValues.RemoveRange(values);

            dbContext.ConfigurationValues.AddRange(Data
                                                   .Select(kvp => new ConfigurationValue {
                Key = kvp.Key, Value = kvp.Value
            })
                                                   .ToArray());

            dbContext.SaveChanges();
        }
Ejemplo n.º 4
0
        public override void Set(string key, string value)
        {
            var builder = new DbContextOptionsBuilder <ConfigDbContext>();

            Source.OptionsAction(builder);

            using (var dbContext = new ConfigDbContext(builder.Options))
            {
                dbContext.ConfigurationValues.Add(new ConfigurationValue()
                {
                    Key = key, Value = value
                });
                dbContext.SaveChanges();
            }
            Load();
        }
Ejemplo n.º 5
0
        public override void Load()
        {
            var builder = new DbContextOptionsBuilder <ConfigDbContext>();

            Source.OptionsAction(builder);

            using (var dbContext = new ConfigDbContext(builder.Options))
            {
                //dbContext.Database.EnsureCreated();
                if (dbContext.Database.EnsureCreated())
                {
                }
                Data = dbContext.ConfigurationValues.Any()
                    ? dbContext.ConfigurationValues.ToDictionary(c => c.Key, c => c.Value)
                    : InitData(dbContext);
            }
        }