public int SavePersonGraph(PersonPOCO personPOCO) { int numRowsAffected = 0; Person person = PersonMapper.MapOneDALtoEF(personPOCO); try { using (var context = new AdventureWorks2012Entities()) { context.Set<Person>().Add(person); var entry = context.ChangeTracker.Entries<Person>().Where(e => e.Entity.BusinessEntityID == personPOCO.BusinessEntityID).FirstOrDefault(); entry.State = ConvertState(personPOCO.CrudState); if (personPOCO.CrudState == CrudState.Unchanged) { var databaseValues = entry.GetDatabaseValues(); entry.OriginalValues.SetValues(databaseValues); } numRowsAffected = context.SaveChanges(); } } catch (System.Data.Entity.Infrastructure.DbUpdateException ex) { if (ex.InnerException != null && ex.InnerException is System.Data.SqlClient.SqlException && ((System.Data.SqlClient.SqlException)ex.InnerException).ErrorCode == 8152) throw ex.InnerException; else throw ex; } return numRowsAffected; }
// For Edit and Delete public HttpResponseMessage PutPerson(PersonPOCO personPOCO) { if (ModelState.IsValid) { try { int numRowsAffected = businessLayer.SavePersonGraph(personPOCO); var response = Request.CreateResponse<int>(HttpStatusCode.OK, numRowsAffected); return response; } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotModified, ex.Message); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
// For Add public HttpResponseMessage PostProduct(PersonPOCO personPOCO) { if (ModelState.IsValid) { try { int numRowsAffected = businessLayer.SavePersonGraph(personPOCO); var response = Request.CreateResponse<int>(HttpStatusCode.OK, numRowsAffected); string uri = Url.Link("DefaultApi", new { BusinessEntityId = personPOCO.BusinessEntityID }); response.Headers.Location = new Uri(uri); return response; } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotModified, ex.Message); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public int SavePersonGraph(PersonPOCO personPOCO) { int numRowsAffected = personRepository.SavePersonGraph(personPOCO); return numRowsAffected; }
public static Person MapOneDALtoEF(PersonPOCO dalPersonPOCO) { var mapper = config.CreateMapper(); Person efPerson = mapper.Map<Person>(dalPersonPOCO); return efPerson; }