public CommonResponseViewModel AddCustomer(CustomerViewModel customerVM) { CommonResponseViewModel response = new CommonResponseViewModel(); try { var cust = new EfDbContext.Customer(); cust.FirstName = customerVM.firstName; cust.MiddleName = customerVM.middleName; cust.NickName = customerVM.nickName; cust.LastName = customerVM.lastName; cust.Mobile = customerVM.mobile; cust.ReferredBy = customerVM.referredBy; cust.CreatedBy = customerVM.createdBy; cust.ModifiedBy = customerVM.modifiedBy; using (_dbContext = new AccountdbContext()) { //Save the customer entity first........ _dbContext.Customer.Add(cust); _dbContext.SaveChanges(); response.isSuccess = true; //update the custid from db to viewModel............................ response.recordId = cust.CustId; customerVM.id = response.recordId; EfDbContext.CustomerDetails customerDetails = new EfDbContext.CustomerDetails() { CustId = customerVM.id, AlternateMobile = customerVM.alternateMobile, HomePhone = customerVM.homePhone, OfficePhone = customerVM.officePhone, Email = customerVM.email, Address = customerVM.address, City = customerVM.city, State = customerVM.state, ShopName = customerVM.shopName, ShopLocation = customerVM.shopLocation }; _dbContext.CustomerDetails.Add(customerDetails); _dbContext.SaveChanges(); //Loop all the address list and update the address in the db.................... //if (customerVM.CustAddressList != null && customerVM.CustAddressList.Any()) //{ // foreach (var item in customerVM.CustAddressList) // { // _dbContext.CustomerDetails.Add(ConstructCustAddressAsPerContext(item, customerVM.id)); // } //} } } catch (Exception ex) { throw ex; } return(response); }
private static CustomerViewModel ConstructCustomerViewModelFromContext(EfDbContext.Customer c, EfDbContext.CustomerDetails cd) { return(new CustomerViewModel() { id = c.CustId, nickName = c.NickName, firstName = c.FirstName, middleName = c.MiddleName, lastName = c.LastName, mobile = c.Mobile, referredBy = c.ReferredBy, createdDate = c.CreatedDate, formattedCreatedDate = c.CreatedDate.ToString(_dateFormat), createdBy = c.CreatedBy, modifiedDate = c.ModifiedDate, formattedModifiedDate = c.ModifiedDate.ToString(_dateFormat), modifiedBy = c.ModifiedBy, alternateMobile = cd.AlternateMobile, homePhone = cd.HomePhone, officePhone = cd.OfficePhone, email = cd.Email, address = cd.Address, city = cd.City, state = cd.State, shopName = cd.ShopName, shopLocation = cd.ShopLocation }); }