Exemplo n.º 1
0
        private static async Task SeedToDoListAsync(ToDoListDbContext toDoBbContext)
        {
            if (toDoBbContext.ToDoList.Any())
            {
                return;
            }
            var user = toDoBbContext.User.FirstOrDefault();

            if (user != null)
            {
                var toDoList = new List <ToDoList>()
                {
                    new ToDoList()
                    {
                        Name = "First List", User = user, CreatedDate = DateTime.UtcNow, DataStatus = 0
                    },
                    new ToDoList()
                    {
                        Name = "Second List", User = user, CreatedDate = DateTime.UtcNow, DataStatus = 0
                    },
                    new ToDoList()
                    {
                        Name = "Third list", User = user, CreatedDate = DateTime.UtcNow, DataStatus = 0
                    },
                };
                toDoBbContext.ToDoList.AddRange(toDoList);
                await toDoBbContext.SaveChangesAsync();
            }
        }
Exemplo n.º 2
0
        private static async Task SeedToDoItemsAsync(ToDoListDbContext toDoBbContext)
        {
            if (toDoBbContext.ToDoItem.Any())
            {
                return;
            }

            var toDoList = toDoBbContext.ToDoList.FirstOrDefault();

            var toDoItems = new List <ToDoItem>()
            {
                new ToDoItem()
                {
                    Description = "First ToDo Item", ToDoList = toDoList, Status = 0, DeadLine = DateTime.UtcNow.AddDays(30), CreatedDate = DateTime.UtcNow, DataStatus = 0
                },
                new ToDoItem()
                {
                    Description = "Second ToDo Item", ToDoList = toDoList, Status = 0, DeadLine = DateTime.UtcNow.AddDays(30), CreatedDate = DateTime.UtcNow, DataStatus = 0
                },
                new ToDoItem()
                {
                    Description = "Third ToDo Item", ToDoList = toDoList, Status = 0, DeadLine = DateTime.UtcNow.AddDays(30), CreatedDate = DateTime.UtcNow, DataStatus = 0
                },
            };

            toDoBbContext.ToDoItem.AddRange(toDoItems);
            await toDoBbContext.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public static async Task SeedAsync(ToDoListDbContext ToDoBbContext, ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                // NOTE : Only run this if using a real database
                // aspnetrunContext.Database.Migrate();
                // aspnetrunContext.Database.EnsureCreated();

                // categories - specifications - reviews - tags
                await SeedToDoListAsync(ToDoBbContext);
                await SeedToDoItemsAsync(ToDoBbContext);
            }
            catch (Exception exception)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <ToDoListDbContextSeed>();
                    log.LogError(exception.Message);
                    //await SeedAsync(ToDoBbContext, loggerFactory, retryForAvailability);
                }
                throw;
            }
        }