Exemplo n.º 1
0
        public async Task <T> AddAsync(T entity)
        {
            _dbContext.Set <T>().Add(entity);
            await _dbContext.SaveChangesAsync();

            return(entity);
        }
Exemplo n.º 2
0
        public static async Task SeedAsync(CollegeContext collegeContext,
                                           ILoggerFactory loggerFactory, int?retry = 0)
        {
            int retryForAvailability = retry.Value;

            try
            {
                if (!collegeContext.Departments.Any())
                {
                    collegeContext.Departments.AddRange(GetExistingDepartments());
                    await collegeContext.SaveChangesAsync();

                    collegeContext.Students.AddRange(GetExistingStudents());
                    await collegeContext.SaveChangesAsync();

                    collegeContext.Instructors.AddRange(GetExistingInstructors());
                    await collegeContext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    var log = loggerFactory.CreateLogger <CollegeContextSeed>();
                    log.LogError(ex.Message);
                    await SeedAsync(collegeContext, loggerFactory, retryForAvailability);
                }
            }
        }