Base solution for your next web application
Open Closed

Deleting Operation not working for Entity Framework Object #757


User avatar
0
mcmaestro created

For some reason when I try to delete an entity object it will not actually delete from the database.

For Example here is my Application Service TemplateAppService, I have a method that called DeleteTemplate and after it runs successfully, I know in the database that the record is not actually delete.

I have even tried to run SaveChanges method manually.

Any help would be appreciated.

Thanks,

public class TemplateAppService : KameleonAppServiceBase, ITemplateAppService
    {
        private readonly IRepository<Template, long> _templateRepository;
        private readonly IRepository<TemplateStatus> _templateStatusRepository;
        private readonly IRepository<TemplateCategory> _templateCategoryRepository;
        private readonly ISvgCreativeAppService _svgCreativeAppService;

        public TemplateAppService(IRepository<Template, long> templateRepository, ISvgCreativeAppService svgCreativeAppService, IRepository<TemplateStatus> templateStatusRepository, IRepository<TemplateCategory> templateCategoryRepository)
        {
            _templateRepository = templateRepository;
            _svgCreativeAppService = svgCreativeAppService;
            _templateStatusRepository = templateStatusRepository;
            _templateCategoryRepository = templateCategoryRepository;
        }

        public async Task<long> CreateTemplate(TemplateInput input)
        {
            var template = new Template();

            input.MapTo(template);

           var id  =  await _templateRepository.InsertAndGetIdAsync(template);

          
               await _svgCreativeAppService.CreateSvgCreative(
                      new SvgCreativeInput
                      {
                          Name = template.Name,
                          TemplateId = id,
                          BaseProductId = input.BaseProductId
                      }
                  );

               await CurrentUnitOfWork.SaveChangesAsync();          

           return id;
        }

        public async Task UpdateTemplate(TemplateInput input)
        {
            var template = await _templateRepository.GetAsync(input.Id);

            if (template == null)
            {
                throw new UserFriendlyException("Product Service Not Found!");
            }

            input.MapTo(template);

            await _templateRepository.UpdateAsync(template);
        }

        public async Task DeleteTemplate(EntityRequestInput<long> input)
        {
            var template = await _templateRepository.GetAsync(input.Id);

            if (template == null)
            {
                throw new UserFriendlyException("Template not found!");
            }

            await _templateRepository.DeleteAsync(template);

            await CurrentUnitOfWork.SaveChangesAsync();
        }

        public async Task<int> CreateTemplateStatus(TemplateStatusInput input)
        {
            var templatestatus = new TemplateStatus();
            
            input.MapTo(templatestatus);

            return await _templateStatusRepository.InsertAndGetIdAsync(templatestatus);

          
        }

        public async Task UpdateTemplateStatus(TemplateStatusInput input)
        {
            var templatestatus = await _templateStatusRepository.GetAsync(input.Id);

            if (templatestatus == null)
            {
                throw new UserFriendlyException("Template Status Not Found!");
            }

            input.MapTo(templatestatus);

            await _templateStatusRepository.UpdateAsync(templatestatus);
        }

        public async Task DeleteTemplateStatus(EntityRequestInput input)
        {
            var templatestatus = await _templateStatusRepository.GetAsync(input.Id);

            if (templatestatus == null)
            {
                throw new UserFriendlyException("Template Status not found!");
            }

            await _templateStatusRepository.DeleteAsync(templatestatus);

            CurrentUnitOfWork.SaveChanges();
        }       
        
        public async Task<TemplateOutput> GetTemplate(EntityRequestInput<long> input)
        {
            var template = await _templateRepository.GetAsync(input.Id);

            if (template == null)
            {
                throw new UserFriendlyException("Template not found!");
            }

            var result = new TemplateOutput();

            template.MapTo(result);

            return result;            
        } 

        public async Task<int> CreateTemplateCategory(TemplateCategoryInput input)
        {
            var templatecategory = new TemplateCategory();

            input.MapTo(templatecategory);

            return await _templateCategoryRepository.InsertAndGetIdAsync(templatecategory);
        }

        public async Task UpdateTemplateCategory(TemplateCategoryInput input)
        {
            var templatecategory = await _templateCategoryRepository.GetAsync(input.Id);

            if (templatecategory == null)
            {
                throw new UserFriendlyException("Template Category Not Found!");
            }

            input.MapTo(templatecategory);

            await _templateCategoryRepository.UpdateAsync(templatecategory);
        }

        public async Task DeleteTemplateCategory(EntityRequestInput input)
        {
            var templatecategory = await _templateCategoryRepository.GetAsync(input.Id);

            if (templatecategory == null)
            {
                throw new UserFriendlyException("Template Category not found!");
            }          

            await _templateCategoryRepository.DeleteAsync(templatecategory);

            await CurrentUnitOfWork.SaveChangesAsync();
        }


        public async Task<TemplateListOutput> GetTemplates(TemplateListInput input)
        {
            var results = await _templateRepository.GetAll().WhereIf(!input.SearchedName.IsNullOrEmpty(), p => p.Name.Contains(input.SearchedName)).OrderBy(x => x.Name)
                            .Skip(input.SkipCount * input.MaxResultCount).Take(input.MaxResultCount).ToListAsync();



            return new TemplateListOutput { Templates = Mapper.Map<List<TemplateOutput>>(results) };
        }

        public async Task<TemplateStatusListOutput> GetTemplatesStatuses(TemplateStatusListInput input)
        {
            var results = await _templateStatusRepository.GetAll().WhereIf(!input.SearchedName.IsNullOrEmpty(), p => p.Name.Contains(input.SearchedName)).OrderBy(x=> x.Name)
                            .Skip(input.SkipCount * input.MaxResultCount).Take(input.MaxResultCount).ToListAsync();



            return new TemplateStatusListOutput { TemplateStatuses = Mapper.Map<List<TemplateStatusOutput>>(results) };
        }

        public async Task<TemplateCategoryListOutput> GetTemplateCategories(TemplateCategoryListInput input)
        {
            var results = await _templateCategoryRepository.GetAll().WhereIf(!input.SearchedName.IsNullOrEmpty(), p => p.Category.Name.Contains(input.SearchedName)).WhereIf(!input.TemplateIdFilter.HasValue, y=> y.TemplateId == input.TemplateIdFilter.Value).OrderBy(x => x.Id)
                            .Skip(input.SkipCount * input.MaxResultCount).Take(input.MaxResultCount).ToListAsync();



            return new TemplateCategoryListOutput { TemplateCategories = Mapper.Map<List<TemplateCategoryOutput>>(results) };
        }
    }

2 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Your code seems simple and true. Are other methods run properly? Are you checking the right database :) Sorry, but this is the only thing I can guess.

  • User Avatar
    0
    mcmaestro created

    I figured out the issue. My Template entity was being referenced in other database tables but deleting entities with casade was disabled for this table. So the database was throwing an error but it was never making it to the fronted.

    Thanks for your help.