public List <CountryViewModel> GetData() { List <CountryViewModel> List = new List <CountryViewModel>(); using (this.context = new model()) { var collection = (from p in this.context.Countries select p).ToList(); foreach (Country item in collection) { using (CountryViewModel i = new CountryViewModel()) { i.MapDataObjectToPresentation(i, item); List.Add(i); } } } return(List); }
public int Update(ArtworkViewModel obj) { Int32 rowsAffected = 0; try { if (obj.Photo != null) { using (this.context = new Artwork_App.ArtWorkDBEntities1()) { Artwork DataObject = new Artwork(); //map and update DataObject = this.MapPresentationObjectToDataObject(obj); //set edit date DataObject.RecordLastUpdateDate = DateTime.Now; this.context.Artworks.Attach(DataObject); this.context.Entry(DataObject).State = System.Data.Entity.EntityState.Modified; rowsAffected = this.context.SaveChanges(); } } else { using (this.context = new Artwork_App.ArtWorkDBEntities1()) { Artwork DataObject = new Artwork(); //map and update DataObject = this.MapPresentationObjectToDataObject(obj); //set edit date //set edit date DataObject.RecordLastUpdateDate = DateTime.Now; context.Artworks.Attach(DataObject); this.context.Entry(DataObject).State = System.Data.Entity.EntityState.Modified; this.context.Entry(DataObject).Property(x => x.Image).IsModified = false; rowsAffected = this.context.SaveChanges(); } } } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (var failure in ex.EntityValidationErrors) { sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType()); foreach (var error in failure.ValidationErrors) { sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); sb.AppendLine(); } } throw new System.Data.Entity.Validation.DbEntityValidationException( "Entity Validation Failed - errors follow:\n" + sb.ToString(), ex ); // Add the original exception as the innerException } //catch (Exception ex) //{ // throw new Exception( ex.Message); //} return(rowsAffected); }