示例#1
0
 public Person GetById(int id)
 {
     using (var context = new PersonDbContext())
     {
         return(context.Persons.Where(p => p.Id == id).FirstOrDefault());
     }
 }
示例#2
0
 public IEnumerable <Person> GetAll()
 {
     using (var context = new PersonDbContext())
     {
         return(context.Persons.ToArray());
     }
 }
示例#3
0
 public Person Add(Person p)
 {
     using (var context = new PersonDbContext())
     {
         context.Persons.Add(p);
         context.SaveChanges();
         return(p);
     }
 }
示例#4
0
 public bool Delete(int id)
 {
     using (var context = new PersonDbContext())
     {
         var person = context.Persons.Where(p => p.Id == id).FirstOrDefault();
         if (person == null)
         {
             return(false);
         }
         context.Persons.Remove(person);
         context.SaveChanges();
         return(true);
     }
 }
示例#5
0
        public Person Update(int id, Person update)
        {
            using (var context = new PersonDbContext())
            {
                var person = context.Persons.Where(p => p.Id == id).FirstOrDefault();
                if (person == null)
                {
                    return(null);
                }
                person.Age  = update.Age;
                person.Name = update.Name;

                context.SaveChanges();
                return(person);
            }
        }