예제 #1
0
 public void AddPerson(string name, int age)
 {
     using (PersonContex pc = new PersonContex())
     {
         Person person = new Person {
             Name = name, Age = age
         };
         pc.People.Add(person);
         pc.SaveChanges();
     }
 }
예제 #2
0
 public void DeletePerson(int idObject)
 {
     using (PersonContex pc = new PersonContex())
     {
         Person person = new Person()
         {
             Id = idObject
         };
         pc.People.Attach(person);
         pc.People.Remove(person);
         pc.SaveChanges();
     }
 }
예제 #3
0
 public void ShowPeople()
 {
     using (PersonContex pc = new PersonContex())
     {
         var people = pc.People.ToList();
         Console.WriteLine("Id" + "\t" + "Name" + "\t" + "Age");
         foreach (var p in people)
         {
             Console.WriteLine("{0}\t{1}\t{2}\t", p.Id, p.Name, p.Age);
         }
         Console.ReadKey();
     }
 }
예제 #4
0
        public void UpdateObject(int idObject, string nameColumn, string newValue)
        {
            using (PersonContex pc = new PersonContex())
            {
                Person person = new Person()
                {
                    Id = idObject
                };
                pc.People.Attach(person);

                if (nameColumn == "Name")
                {
                    person.Name = newValue;
                }

                if (nameColumn == "Age")
                {
                    int newAge = Int32.Parse(newValue);
                    person.Age = newAge;
                }
                pc.SaveChanges();
            }
        }