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(); }