public static Address GetAddress(this ManagerDTO ManagerDTO) { return(new Address { CellPhoneNo = ManagerDTO.CellPhoneNo, AddressType = (int)AddressType.eManager, Email = ManagerDTO.Email, Fax = ManagerDTO.Fax, HouseNo = ManagerDTO.HouseNo, IsActive = ManagerDTO.IsActive, IsDeleted = ManagerDTO.IsDeleted, IsMainOffice = true, KebeleId = ManagerDTO.KebeleId, OtherAddress = ManagerDTO.OtherAddress, ParentId = ManagerDTO.ManagerId, RegionId = ManagerDTO.RegionId, Pobox = ManagerDTO.Pobox, Remark = "", SpecificAreaName = "", TeleNo = ManagerDTO.TeleNo, TownId = "-1", WoredaId = ManagerDTO.WoredaId, ZoneId = ManagerDTO.ZoneId, AddressId = ManagerDTO.AddressId }); }
public async Task <ManagerDTO> SaveManager(ManagerDTO postedManager) { bool isUpdate = (postedManager.ManagerId > 0); // New or Update? // map DTO to manager entity Manager mgr = ManagerHelper.GetManager(postedManager); mgr.CreatedDate = DateTime.Now; // We should always use transaction for operations involving two or more entities using (var transaction = await context.Database.BeginTransactionAsync()) { try { // first save manager entity if (isUpdate) { mgr.ManagerId = postedManager.ManagerId; context.Manager.Update(mgr); context.SaveChanges(); } else { context.Manager.Add(mgr); context.SaveChanges(); } // then save address entity Address address = ManagerHelper.GetAddress(postedManager); address.ParentId = mgr.ManagerId; if (isUpdate) { address.AddressId = postedManager.AddressId; context.Address.Update(address); context.SaveChanges(); } else { address.AddressId = 0; context.Address.Add(address); context.SaveChanges(); } } catch (Exception ex) { transaction.Rollback(); throw new Exception(ex.Message); } transaction.Commit(); postedManager.ManagerId = mgr.ManagerId; //used for image filename return(postedManager); } }
public static Manager GetManager(this ManagerDTO managerDTO) { try { return(new Manager { //ManagerId = (managerDTO.ManagerId > 0) ? 0 : managerDTO.ManagerId, //DateOfBirth = managerDTO.DateOfBirth, FatherName = managerDTO.FatherName, FatherNameEng = managerDTO.FatherNameEng, FatherNameSort = string.Empty, FatherNameSoundx = string.Empty, FirstName = managerDTO.FirstName, FirstNameEng = managerDTO.FirstNameEng, FirstNameSort = string.Empty, FirstNameSoundx = string.Empty, Gender = managerDTO.Gender, GrandName = managerDTO.GrandName, GrandNameEng = managerDTO.GrandNameEng, GrandNameSort = string.Empty, GrandNameSoundx = string.Empty, IsActive = managerDTO.IsActive, IsDeleted = managerDTO.IsDeleted, Nationality = managerDTO.Nationality, Origin = managerDTO.Origin, Remark = string.Empty, Title = managerDTO.Title, Tin = managerDTO.Tin, CustomerId = managerDTO.CustomerId }); } catch (Exception ex) { string s = ex.Message; return(null); } }