Пример #1
0
 // GET api/<controller>
 public IEnumerable<Contact> Get()
 {
     using (var db = new AddressBookEntities())
     {
         return db.Contacts.ToArray();
     }
 }
Пример #2
0
 // GET api/<controller>/5
 public Contact Get(int id)
 {
     using (var db = new AddressBookEntities())
     {
         return db.Contacts.Include("Addresses").SingleOrDefault(c => c.ContactId == id);
     }
 }
Пример #3
0
 // POST api/<controller>
 public HttpResponseMessage Post([FromBody]Contact value)
 {
     using (var db = new AddressBookEntities())
     {
         var contact = db.Contacts.Add(value);
         db.SaveChanges();
         var response = Request.CreateResponse(HttpStatusCode.Created, contact);
         response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = contact.ContactId }));
         return response;
     }
 }
Пример #4
0
        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            using (var db = new AddressBookEntities())
            {
                var contact = db.Contacts.Find(id);

                // Remove existing contact
                if (contact != null)
                {
                    db.Contacts.Remove(contact);
                    db.SaveChanges();
                }
            }
        }
Пример #5
0
        // PUT api/<controller>/5
        public void Put(int id, [FromBody]Contact value)
        {
            using (var db = new AddressBookEntities())
            {
                var contact = db.Contacts.Find(id);

                // Update existing contact
                if (contact != null)
                {
                    contact.FirstName = value.FirstName;
                    contact.MiddleName = value.MiddleName;
                    contact.LastName = value.LastName;
                    contact.Suffix = value.Suffix;
                    contact.DOB = value.DOB;
                    db.SaveChanges();
                }
            }
        }