public InstituteEditModel GetInstituteEditModel(Institute institute) { InstituteDetailsModel details = new InstituteDetailsModel { ID = institute.ID, Name = institute.Name, InMailingList = institute.InMailingList, MainPhone = institute.MainPhone, Phones = institute.Phones, Email = institute.Email, City = institute.City, ZipCode = institute.ZipCode, Address = institute.Address, Address_Comments = institute.Address_Comments, Institute_Comments = institute.Comments, }; InstituteEditModel editModel = new InstituteEditModel(); editModel.details = details; return(editModel); }
public void CreateUpdate(InstituteDetailsModel model, bool isUpdate) { string operation = tbl + " Create Update "; Logger.Log.Debug(operation + " Begin "); var institute = new Institute(); if (isUpdate) { institute = dbContext.Institutes.SingleOrDefault(f => f.ID == model.ID); if (institute == null) { throw new Exception("האדם לא קיים"); } } //mandatory if (string.IsNullOrEmpty(model.Name)) { throw new Exception("שם חובה"); } institute.Name = model.Name; institute.Address = model.Address; institute.Address_Comments = HttpUtility.HtmlDecode(model.Address_Comments); institute.City = model.City; institute.Email = model.Email; institute.InMailingList = model.InMailingList; institute.MainPhone = model.MainPhone; institute.Comments = HttpUtility.HtmlDecode(model.Institute_Comments); institute.Phones = model.Phones; institute.ZipCode = model.ZipCode; try { if (!isUpdate) { dbContext.Institutes.Add(institute); } dbContext.SaveChanges(); } catch (DbEntityValidationException e) { string msgs = ""; foreach (var eve in e.EntityValidationErrors) { Logger.Log.ErrorFormat("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Logger.Log.ErrorFormat("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); msgs += ": " + ve.ErrorMessage; } } throw new Exception("הייתה בעיה בשמירת הנתונים" + msgs); } catch (Exception ex) { string msg = ""; if (ex.InnerException != null) { var inner = ex.InnerException; while (inner != null) { if (inner.InnerException != null) { inner = inner.InnerException; } else { break; } } msg = inner.Message; } else { msg = ex.Message; } throw new Exception("הייתה בעיה בשמירת הנתונים: " + msg); } Logger.Log.Debug(operation + " End "); }