public ActionResult Delete(long id, State vm)
        {
            ModelState.Clear();
            State state = this.db.States
                                  .Where(c => c.Id == vm.Id)
                                  .FirstOrDefault();

            if (state == null)
                return HttpNotFound();

            if (state.Cities.Count > 0)
            {
                this.FlashError("Você não pode apagar esse estado pois ele tem cidades cadastradas. Remova todas as cidades antes de apagar o estado");
                return View(state);
            }

            try
            {
                db.States.Remove(state);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                this.FlashError("Ocorreu um problema ao apagar o estado");
            }

            return View(state);
        }
        public ActionResult Create(VMEditCreateState vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    State state = new State();
                    state.Name = vm.Name;
                    state.Code = vm.Code;
                    state.Country = db.Countries.Where(c => c.Id == vm.CountryId).FirstOrDefault();
                    this.db.States.Add(state);
                    this.db.SaveChanges();
                    this.FlashInfo("Estado cadastrado com sucesso.");
                    return RedirectToAction("Index");
                }
                catch (DbEntityValidationException ex)
                {
                    foreach(var entityError  in ex.EntityValidationErrors)
                    {
                        foreach(var error in entityError.ValidationErrors)
                        {
                            ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
                        }
                    }
                }catch(Exception ex)
                {
                    ModelState.AddModelError("", "Ocorreu um problema ao salvar o estado");
                }

            }

            vm.Countries = db.Countries.OrderBy(m => m.Name).ToList();
            return View(vm);
        }
        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();

            }
        }