public static async Task SeedAsync(CollegeContext collegeContext, ILoggerFactory loggerFactory, int?retry = 0) { int retryForAvailability = retry.Value; try { collegeContext.Database.Migrate(); if (!collegeContext.Claims.Any()) { collegeContext.Claims.AddRange( GetPreconfiguredClaims()); await collegeContext.SaveChangesAsync(); } if (!collegeContext.Roles.Any()) { collegeContext.Roles.AddRange( GetPreconfiguredRoles()); await collegeContext.SaveChangesAsync(); } if (!collegeContext.RoleClaims.Any()) { collegeContext.RoleClaims.AddRange( GetPreconfiguredRoleClaims(collegeContext)); await collegeContext.SaveChangesAsync(); } if (!collegeContext.AppUsers.Any()) { collegeContext.AppUsers.AddRange( GetPreconfiguredAppUsers(collegeContext)); await collegeContext.SaveChangesAsync(); } } catch (Exception ex) { if (retryForAvailability < 10) { retryForAvailability++; var log = loggerFactory.CreateLogger <CollegeContextSeed>(); log.LogError(ex.Message); await SeedAsync(collegeContext, loggerFactory, retryForAvailability); } } }
public async Task <T> AddAsync(T entity, Guid appUserId) { await _dbContext.Set <T>().AddAsync(entity); if (typeof(T).IsSubclassOf(typeof(BaseEntityFull))) { var baseEntity = entity as BaseEntityFull; baseEntity.SetUserAddProperties(appUserId); } if (typeof(T).IsSubclassOf(typeof(BaseEntityDateStamps))) { var baseEntity = entity as BaseEntityDateStamps; baseEntity.SetDateAddProperties(); } await _dbContext.SaveChangesAsync(); return(entity); }