public ActionResult MigrateOpenliga()
        {
            var m = new MigrationResultModel()
            {
                CurrentLeagueSeason   = SportsdataConfigInfo.Current.LeagueSaison,
                CurrentLeagueShortcut = SportsdataConfigInfo.Current.LeagueShortcut
            };

            return(View(m));
        }
        public ActionResult MigrateOpenliga(string newLeagueSeason, string newLeagueShortcut)
        {
            var m = new MigrationResultModel()
            {
                CurrentLeagueSeason   = SportsdataConfigInfo.Current.LeagueSaison,
                CurrentLeagueShortcut = SportsdataConfigInfo.Current.LeagueShortcut
            };

            // precondition: db exists under new name
            {
                if (_matchDataRepository.Exist(newLeagueShortcut, newLeagueSeason))
                {
                    log.DebugFormat("League {0}/{1} is valid", newLeagueShortcut, newLeagueSeason);

                    var newClient = new WMFussballDataRepository(newLeagueShortcut, newLeagueSeason);

                    var newMatches = newClient.GetMatchesByCurrentGroup();

                    using (var ctxt = new TippSpielContext())
                    {
                        var migrationList = new List <TippMatchModel>();

                        foreach (var tipp in ctxt.TippMatchList)
                        {
                            //
                            // Basic idea:
                            // 1. is match tipp among the matches to be migrated, if yes it is already migrated => finish
                            // 2. if no, then migrate it but only if tipp belongs to old league. Do not touch others
                            //

                            var newMatchObj = (from t in newMatches
                                               where t.MatchId == tipp.MatchId
                                               select t)
                                              .FirstOrDefault();

                            if (newMatchObj == null)
                            {
                                var oldMatchObj = _matchDataRepository.GetMatchData(tipp.MatchId);

                                if (oldMatchObj != null)
                                {
                                    // find corresponding match in new league
                                    var newMatchByTeams = (from t in newMatches
                                                           where t.HomeTeamId == oldMatchObj.HomeTeamId &&
                                                           t.AwayTeamId == oldMatchObj.AwayTeamId
                                                           select t)
                                                          .FirstOrDefault();

                                    if (newMatchByTeams != null)
                                    {
                                        using (var ctxt2 = new TippSpielContext())
                                        {
                                            var tippObj = (from t in ctxt2.TippMatchList
                                                           where t.User == tipp.User &&
                                                           t.MatchId == newMatchByTeams.MatchId
                                                           select t)
                                                          .FirstOrDefault();

                                            if (tippObj == null) // not null means tipp has already been migrated
                                            {
                                                var newTipp = new TippMatchModel()
                                                {
                                                    MatchId     = newMatchByTeams.MatchId,
                                                    GroupId     = tipp.GroupId,
                                                    IsJoker     = tipp.IsJoker,
                                                    LastUpdated = DateTime.Now,
                                                    MyAmount    = tipp.MyAmount,
                                                    MyOdds      = tipp.MyOdds,
                                                    MyTip       = tipp.MyTip,
                                                    User        = tipp.User
                                                };

                                                migrationList.Add(newTipp);
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        // save migration list
                        foreach (var t in migrationList)
                        {
                            log.DebugFormat("Migrate match={0} for user={1}", t.MatchId, t.User);

                            ctxt.TippMatchList.Add(t);
                        }

                        ctxt.SaveChanges();
                    }
                }
                else
                {
                    var errMsg = String.Format("League {0}/{1} does not exist", newLeagueShortcut, newLeagueSeason);
                    log.Debug(errMsg);

                    ModelState.AddModelError("league", errMsg);
                }
            }

            return(View(m));
        }