public void UpdateAddress([FromUri] int id, [FromBody] AddressBookDto obj) { var bookInDb = _context.FBAAddressBooks.Find(id); //如果当前要修改对象的CODE与数据库的不同且其他对象有相同的CODE,说明CODE已经被其他对象使用了,返回异常 if (bookInDb.WarehouseCode != obj.AddressCode && _context.FBAAddressBooks.Where(x => x.WarehouseCode == obj.AddressCode).Count() > 0) { throw new Exception("Code " + obj.AddressCode + " has been taken. Please try another one."); } bookInDb.WarehouseCode = obj.AddressCode; bookInDb.Address = obj.AddressDetail; bookInDb.Memo = obj.Memo; _context.SaveChanges(); }
public IHttpActionResult CreateNewAddressBook([FromBody] AddressBookDto obj) { if (_context.FBAAddressBooks.Where(x => x.WarehouseCode == obj.AddressCode).Count() != 0) { throw new Exception("Code " + obj.AddressCode + " has been taken. Please try another one."); } var address = new FBAAddressBook { WarehouseCode = obj.AddressCode, Address = obj.AddressDetail, Memo = obj.Memo }; _context.FBAAddressBooks.Add(address); _context.SaveChanges(); var resultDto = Mapper.Map <FBAAddressBook, FBAAddressBookDto>(_context.FBAAddressBooks.OrderByDescending(x => x.Id).First()); return(Created(Request.RequestUri + "/" + resultDto.Id, resultDto)); }