public void MigrateUsers() { var members = ctx1.Members.ToList(); TotalTicks = members.Count; Console.WriteLine($"Members to migrate: {TotalTicks}"); using var pbar = new ProgressBar(TotalTicks, "Data migration progress", ProgressBarOptions); ctx2.Database.BeginTransaction(); var ct = 0; foreach (var member in members) { pbar.Message = $"Start {ct} of {TotalTicks}"; var user = ctx2.User.FirstOrDefault(x => x.Phone == member.Phone.ToString()); if (user == null) { var skill = member.SkillId != null?ctx1.Skills.FirstOrDefault(x => x.Id == member.SkillId)?.Skill : "brak wyszkolenia"; var newUser = new User { Phone = member.Phone.ToString(), Password = member.Token, Roles = "[]", Skill = skill }; ctx2.User.Add(newUser); ctx2.SaveChanges(); } pbar.Tick($"Inserted rows {ct} of {TotalTicks}"); ct++; } ctx2.Database.CommitTransaction(); }
private static void MigrateUserAssociation(MemberAssociation item) { var db1 = new SourceDbContext(); var db2 = new HostDbContext(); var member = db1.Members.FirstOrDefault(m => m.MemberId == item.MemberId); if (member != null) { var user = db2.User.FirstOrDefault(x => x.Phone == member.Phone.ToString()); var unit = db2.Units.FirstOrDefault(x => x.UnitId == item.UnitId); if (user != null && unit != null) { var isAssociated = db2.UserAssociation.FirstOrDefault(x => x.UnitId == unit.Id && x.IdUser == user.Id); if (isAssociated == null) { var ua = new UserAssociation { IdUser = user.Id, UnitId = unit.Id }; db2.UserAssociation.Add(ua); db2.SaveChanges(); } } } db1.Dispose(); db2.Dispose(); }