public IHttpActionResult PutCourse(int id, Course course) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != course.CourseId) { return(BadRequest()); } db.Entry(course).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CourseExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
private static void CreateBlog() { // Create and save a new Blog Console.Write("Enter a name for a new Blog: "); var name = Console.ReadLine(); var blog = new Blog { Name = name }; db.Blogs.Add(blog); db.SaveChanges(); // Display all Blogs from the database var query = from b in db.Blogs orderby b.Name select b; Console.WriteLine("All blogs in the database:"); foreach (var item in query) { Console.WriteLine(item.Name); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
public void DeleteAllTrains() { using (var db = new TrainContext()) { db.Trains.ToList().ForEach(x => db.Trains.Remove(x)); db.SaveChanges(); } }
public void Delete(int id) { using (var db = new TrainContext()) { var trains = db.Trains.Where(x => x.TrainSymbol == "Mustafa Train"); db.Trains.RemoveRange(trains); db.SaveChanges(); } }
public ActionResult Create([Bind(Include = "DriverTeamID,DriverGroupID,DriverName,Sex,Birthday,DriveType")] Driver driver) { try { if (ModelState.IsValid) { db.Drivers.Add(driver); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException /* dex */) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } ViewBag.DriverGroupID = new SelectList(db.DriverGroups, "DriverGroupID", "DriverGroupName", driver.DriverGroupID); return(View(driver)); }
public ActionResult Create([Bind(Include = "TeamName,GroupNumber,remark")] DriverTeam driverteam) { try { if (ModelState.IsValid) { db.DriverTeams.Add(driverteam); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException /*dex*/) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Tryagain, and if the problem persists see your system administrator."); } return(View(driverteam)); }
public static void Main(string[] args) { db = new TrainContext(); //设置课程和学生数据 // Course(); setStudens(); db.SaveChanges(); queuryStudens("猫猫"); queuryStudens("坤坤"); Console.ReadKey(); }
public ActionResult Create([Bind(Include = "DriverTeamID,DriverGroupName,GroupPeople")] DriverGroup drivergroup) { try { if (ModelState.IsValid) { db.DriverGroups.Add(drivergroup); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException /*dex*/) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Tryagain, and if the problem persists see your system administrator."); } ViewBag.DriverTeamID = new SelectList(db.DriverTeams, "DriverTeamID", "TeamName", drivergroup.DriverTeamID); return(View(drivergroup)); }
public void Edit(int id, string trainSymbol, int speed = 0, string description = "") { using (var db = new TrainContext()) { var train = db.Trains.FirstOrDefault(x => x.TrainID == id); if (train != null) { train.TrainSymbol = trainSymbol; train.Speed = speed; train.Description = description; db.SaveChanges(); } } }
public void Add(Train train, TrainStation trainStation) { using (var db = new TrainContext()) { if (!(db.Trains.Any(x => x.TrainSymbol == train.TrainSymbol) || db.TrainStations.Any(x => x.StationName == trainStation.StationName))) { db.Trains.Add(new Train { TrainSymbol = train.TrainSymbol, Speed = train.Speed, Description = train.Description }); db.TrainStations.Add(new TrainStation { StationName = trainStation.StationName, StationAddress = trainStation.StationAddress }); db.SaveChanges(); } } }