public int Insert(FamilyViewModel familyViewModel, List <ChildViewModel> childViewModel) { AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap <FamilyViewModel, Family>()); var family = AutoMapper.Mapper.Map <FamilyViewModel, Family>(familyViewModel); AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap <List <ChildViewModel>, List <Child> >()); var children = AutoMapper.Mapper.Map <List <ChildViewModel>, List <Child> >(childViewModel); connectionFactory = ConnectionHelper.GetConnection(); var context = new DbContext(connectionFactory); var familyRepository = new FamilyRepository(context); var childRepository = new ChildRepository(context); int ParentId = familyRepository.Insert(family); childRepository.Insert(children, ParentId); return(ParentId); }
// POST api/<controller> public void Post([FromBody] List <Family> families) { using (SqlUnitOfWork unitOfWork = new SqlUnitOfWork("gp")) { FamilyRepository familyRepo = new FamilyRepository(unitOfWork); foreach (Family family in families) { if (family.FamilyId == null) { familyRepo.Insert(family); } else { familyRepo.Update(family); } } unitOfWork.Commit(); } }