public IHttpActionResult Post(LandTypeViewModel item_viewModel) { if (!ModelState.IsValid) { return BadRequest(ModelState); } LandType item = new LandType { Id = item_viewModel.Id, Type=item_viewModel.Type}; //不需指定Timestamp欄位 db.LandTypes.Add(item); try { db.SaveChanges(); } catch (DbEntityValidationException ex) { var entityError = ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).Select(x => x.ErrorMessage); var getFullMessage = string.Join("; ", entityError); var exceptionMessage = string.Concat(ex.Message, "errors are: ", getFullMessage); throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.BadRequest, exceptionMessage)); } return CreatedAtRoute("DefaultApi", new { id = item.Id }, ToLandTypeViewModel(item)); }
private LandTypeViewModel ToLandTypeViewModel(LandType item) { return new LandTypeViewModel { Id = item.Id, Type = item.Type, TimestampString = Convert.ToBase64String(item.Timestamp) }; }