public void SetBet(string login, int race, string pos1, string pos2, string pos3, string time1) { using (ProjektEntities db = new ProjektEntities()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { BETS itm = new BETS(); itm.User_ID = login; itm.Date = DateTime.Now.ToString(); itm.Race_ID = race; itm.Pos_1 = pos1; itm.Pos_2 = pos2; itm.Pos_3 = pos3; itm.Time_1 = time1; db.BETS.Add(itm); db.SaveChanges(); dbContextTransaction.Commit(); } catch { dbContextTransaction.Rollback(); } } } }
public ActionResult Edytuj_wyscig([Bind(Include = "Race_ID,Season_ID,Track,Date,Pos_1,Time_1,Pos_2,Pos_3")] RACES rACES) { if (ModelState.IsValid) { if (rACES.Pos_1 != rACES.Pos_2 && rACES.Pos_1 != rACES.Pos_3 && rACES.Pos_2 != rACES.Pos_3) { //////////////////////////////////// zapis danych do tabeli RACES var bet = from b in db.BETS join r in db.RACES on b.Race_ID equals r.Race_ID where r.Race_ID == rACES.Race_ID select new { b.Pos_1, b.Pos_2, b.Pos_3, b.Bet_ID, b.User_ID }; var dri1 = from r in db.RACES from dr in db.DRIVERS where r.Pos_1 == dr.Driver_ID && r.Race_ID == rACES.Race_ID select new { dr.Driver_Name }; string pos1 = ""; foreach (var a in dri1) { pos1 = a.Driver_Name; } var dri2 = from r in db.RACES from dr in db.DRIVERS where r.Pos_2 == dr.Driver_ID && r.Race_ID == rACES.Race_ID select new { dr.Driver_Name }; string pos2 = ""; foreach (var a in dri2) { pos2 = a.Driver_Name; } var dri3 = from r in db.RACES from dr in db.DRIVERS where r.Pos_3 == dr.Driver_ID && r.Race_ID == rACES.Race_ID select new { dr.Driver_Name }; string pos3 = ""; foreach (var a in dri3) { pos3 = a.Driver_Name; } var dri4 = from r in db.RACES from dr in db.DRIVERS where r.Time_1 == dr.Driver_ID && r.Race_ID == rACES.Race_ID select new { dr.Driver_Name }; string time1 = ""; foreach (var a in dri4) { time1 = a.Driver_Name; } foreach (var x in bet) { BETS bets = db.BETS.Find(x.Bet_ID); if (bets.Pos_1 == pos1) { bets.ScorePos1 = 5; } else { bets.ScorePos1 = 0; } if (bets.Pos_2 == pos2) { bets.ScorePos2 = 3; } else { bets.ScorePos2 = 0; } if (bets.Pos_3 == pos3) { bets.ScorePos3 = 1; } else { bets.ScorePos3 = 0; } if (bets.Time_1 == time1) { bets.ScoreTime1 = 5; } else { bets.ScoreTime1 = 0; } /////////////////////////////////////////// zapis punktow do tabeli BETS bets.ScoreSum = bets.ScorePos1 + bets.ScorePos2 + bets.ScorePos3 + bets.ScoreTime1; db.Entry(bets).State = EntityState.Modified; } db.Entry(rACES).State = EntityState.Modified; var us = from u in db.USER select u; foreach (var item in us) { item.Total_score = 0; var bets1 = from be in db.BETS where be.User_ID == item.User_ID select new { be.ScoreSum }; foreach (var points in bets1) { item.Total_score += points.ScoreSum; } db.Entry(item).State = EntityState.Modified; } db.SaveChanges(); return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Dane się nie zgadzają."); } } ViewBag.Time_1 = new SelectList(db.DRIVERS, "Driver_ID", "Driver_Name", rACES.Time_1); ViewBag.Season_ID = new SelectList(db.SEASONS, "Season_ID", "Year", rACES.Season_ID); ViewBag.Pos_1 = new SelectList(db.DRIVERS, "Driver_ID", "Driver_Name", rACES.Pos_1); ViewBag.Pos_2 = new SelectList(db.DRIVERS, "Driver_ID", "Driver_Name", rACES.Pos_2); ViewBag.Pos_3 = new SelectList(db.DRIVERS, "Driver_ID", "Driver_Name", rACES.Pos_3); return(View(rACES)); }