Exemplo n.º 1
0
 public static void DeletePerson(int PersonId)
 {
     using (var db = new PersonsToCityContext())
     {
         var ToDelete = db.Persons.FirstOrDefault(x => x.PersonId == PersonId);
         db.Persons.Remove(ToDelete);
         db.SaveChanges();
     }
 }
Exemplo n.º 2
0
        public static Person CreatePerson(string PersonName, int CityId)
        {
            using (var db = new PersonsToCityContext())
            {
                var ToAdd = new Person();
                ToAdd.PersonName = PersonName;
                ToAdd.CityId     = CityId;
                var AddedPerson = db.Persons.Add(ToAdd);
                AddedPerson.City = db.Cities.FirstOrDefault(x => x.CityId == CityId);
                db.SaveChanges();

                return(AddedPerson);
            }
        }
Exemplo n.º 3
0
        public static Person UpdatePerson(int PersonId, string PersonName, int CityId)
        {
            using (var db = new PersonsToCityContext())
            {
                var ToUpdate = db.Persons.FirstOrDefault(x => x.PersonId == PersonId);
                ToUpdate.PersonName = PersonName;
                ToUpdate.CityId     = CityId;
                db.SaveChanges();

                var UpdatedPerson = ToUpdate;

                UpdatedPerson.City = db.Cities.FirstOrDefault(x => x.CityId == CityId);

                return(UpdatedPerson);
            }
        }
Exemplo n.º 4
0
        public static City UpdateCity(int CityId, string CityName)
        {
            using (var db = new PersonsToCityContext())
            {
                var ToUpdate = db.Cities.FirstOrDefault(x => x.CityId == CityId);
                ToUpdate.CityName = CityName;
                db.SaveChanges();

                var UpdatedCity = ToUpdate;
                if (UpdatedCity == null)
                {
                    UpdatedCity.Persons = new List <Person>();
                    foreach (var person in db.Persons.Where(x => x.CityId == UpdatedCity.CityId))
                    {
                        UpdatedCity.Persons.Add(person);
                    }
                }

                return(UpdatedCity);
            }
        }
Exemplo n.º 5
0
        public static City CreateCity(string CityName)
        {
            using (var db = new PersonsToCityContext())
            {
                var ToAdd = new City();
                ToAdd.CityName = CityName;
                var AddedCity = db.Cities.Add(ToAdd);
                db.SaveChanges();

                if (AddedCity.Persons == null)
                {
                    AddedCity.Persons = new List <Person>();
                    foreach (var person in db.Persons.Where(x => x.CityId == AddedCity.CityId))
                    {
                        AddedCity.Persons.Add(person);
                    }
                }

                return(AddedCity);
            }
        }