public SensorsController() { if(this.db.Sensors.Count() == 0) { City city; State state; Country country; if(db.Countries.Count() == 0) { country = new Country { Name = "Brasil" }; db.Countries.Add(country); db.SaveChanges(); } else { country = db.Countries.Where(c => c.Name == "Brasil").FirstOrDefault(); } if(db.States.Count() == 0) { state = new State { Name = "Santa Catarina", Country = country, Code = "SC" }; db.States.Add(state); db.SaveChanges(); }else { state = db.States.Where(s => s.Name == "Santa Catarina").FirstOrDefault(); } if(db.Cities.Count() == 0) { city = new City { Name = "Joinville", State = state }; db.Cities.Add(city); db.SaveChanges(); }else { city = db.Cities.Where(c => c.Name == "Joinville").FirstOrDefault(); } for(int i = 0; i < 100; i++) { Sensor sensor = new Sensor(); sensor.Address = "Rua XV de Novembro " + i; sensor.City = city; sensor.Latitude = 10.0; sensor.Longitude = 10.0; sensor.Name = "Sensor " + i; sensor.SerialNumber = i.ToString(); db.Sensors.Add(sensor); } db.SaveChanges(); } }
public ActionResult Create(Sensor vm) { if (ModelState.IsValid) { try { Sensor sensor = new Sensor(); sensor.Name = vm.Name; sensor.CreatedAt = DateTime.Now; sensor.UpdatedAt = DateTime.Now; this.db.Sensors.Add(sensor); this.db.SaveChanges(); this.FlashInfo("Sensor cadastrado com sucesso."); return RedirectToAction("Index"); } catch(Exception ex) { ModelState.AddModelError("", "Ocorreu um problema ao salvar o sensor"); } } return View(vm); }
public ActionResult Create() { Sensor sensor = new Sensor(); return View(sensor); }