コード例 #1
0
        private bool AddUpdateCategorySubCategoryMapRepo(SubCategoryViewModel viewModel)
        {
            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                using (DbContextTransaction transaction = dbContext.Database.BeginTransaction())
                {

                    DbSet<SubCategory> subCategorySet = dbContext.SubCategorySet;
                    SubCategory subCategory = new SubCategory();
                    ObjectMapper.PropertyMap(viewModel, subCategory);

                    FindResult<CategoryViewModel> categoryResult = categoryRepository.GetAll(dbContext);

                    FindResult<CategorySubCategoryMapViewModel> mapResult = categorySubCategoryMapRepo.FindBy(x => x.SubCategoryId == viewModel.SubCategoryId);

                    List<CategorySubCategoryMapViewModel> existingMappings = new List<CategorySubCategoryMapViewModel>();

                    existingMappings = mapResult.Entities;

                    List<Guid> deletionList = new List<Guid>();

                    List<Guid> newAdded = new List<Guid>();

                    foreach (var emapvm in existingMappings)
                    {
                        if (!viewModel.CategoryIds.Contains(emapvm.CategoryId))
                        {
                            deletionList.Add(emapvm.CategorySubCategoryMapId);
                        }
                    }

                    if (viewModel.CategoryIds != null && viewModel.CategoryIds.Count > 0)
                    {

                        foreach (var cId in viewModel.CategoryIds)
                        {
                            CategorySubCategoryMapViewModel existingMap = existingMappings.FirstOrDefault(x => x.SubCategoryId == viewModel.SubCategoryId && x.CategoryId == cId);

                            if (existingMap == null)
                            {
                                newAdded.Add(cId);
                            }
                        }

                    }

                    if (deletionList.Count > 0)
                    {
                        foreach (Guid mapid in deletionList)
                        {
                            categorySubCategoryMapRepo.Delete(mapid);
                        }
                    }

                    if (newAdded.Count > 0)
                    {
                        foreach (Guid categoryId in newAdded)
                        {
                            CategorySubCategoryMapViewModel catSubCatMapVM = new CategorySubCategoryMapViewModel
                            {

                                CategorySubCategoryMapId = Guid.Empty,
                                CategoryId = categoryId,
                                SubCategoryId = viewModel.SubCategoryId,

                            };

                            categorySubCategoryMapRepo.Update(dbContext, catSubCatMapVM);
                        }
                    }

                    subCategorySet.Attach(subCategory);

                    dbContext.Entry<SubCategory>(subCategory).State = EntityState.Modified;

                    dbContext.SaveChanges();

                    transaction.Commit();
                }
            }

            return true;
        }
コード例 #2
0
        private void AddCategorySubCategoryMap(InnoventoryDBContext dbContext, SubCategoryViewModel viewModel)
        {
            if (viewModel.CategoryIds != null && viewModel.CategoryIds.Count > 0)
            {
                foreach (Guid categoryId in viewModel.CategoryIds)
                {
                    CategorySubCategoryMapViewModel mapViewModel = new CategorySubCategoryMapViewModel
                    {
                        CategorySubCategoryMapId = Guid.Empty,
                        CategoryId = categoryId,
                        SubCategoryId = viewModel.SubCategoryId,
                    };

                    categorySubCategoryMapRepo.Update(dbContext, mapViewModel);
                }
            }
        }