async public void Has_Relations() { // Assign var result = true; var tag = new Tag { FaqTags = new List <FaqTag> { new FaqTag(), }, }; DbContextFake.SeedDb <IntranetApiContext>(c => c.Tags.Add(tag)); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Act result = await TagHelpers.HasNoRelatedEntities(context.Tags.SingleOrDefault(), context); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.False(result); } }
public async Task <IActionResult> DeleteConfirmed(int id) { var faq = await _context.Faqs .Include(f => f.FaqTags) .ThenInclude(ft => ft.Tag) .Include(f => f.Category) .ThenInclude(c => c.Faqs) .SingleOrDefaultAsync(f => f.Id == id); if (faq.IsNull()) { return(NotFound()); } _context.Remove(faq); // If the category has no more related entities then delete the category as well if (await CategoryHelpers.HasNoRelatedEntities(faq.Category, _context, ignore: faq)) { _context.Remove(faq.Category); } // If the tags has no more related entities then delete the category as well foreach (var tag in faq.FaqTags.Select(ft => ft.Tag)) { if (await TagHelpers.HasNoRelatedEntities(tag, _context, ignore: faq.FaqTags)) { _context.Remove(tag); } } await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
async public void No_Relations() { // Assign var result = false; DbContextFake.SeedDb <IntranetApiContext>(c => c.Add(new Tag())); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Act result = await TagHelpers.HasNoRelatedEntities(context.Tags.SingleOrDefault(), context); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.True(result); } }
public async Task <IActionResult> DeleteConfirmed(int id) { var policy = await _context.Policies .Include(p => p.PolicyTags) .ThenInclude(pt => pt.Tag) .Include(p => p.Category) .ThenInclude(c => c.Policies) .SingleOrDefaultAsync(p => p.Id == id); if (policy.IsNull()) { return(NotFound()); } // TODO: Delete blob as well _context.Remove(policy); // If the category has no more related entities then delete the category as well if (await CategoryHelpers.HasNoRelatedEntities(policy.Category, _context, ignore: policy)) { _context.Remove(policy.Category); } // If the tags has no more related entities then delete the tag as well foreach (var tag in policy.PolicyTags.Select(ft => ft.Tag)) { if (await TagHelpers.HasNoRelatedEntities(tag, _context, ignore: policy.PolicyTags)) { _context.Remove(tag); } } await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
async public void Ignore_Relation_Has_No_Other() { // Assign var result = false; DbContextFake.SeedDb <IntranetApiContext>(c => { c.Add(new Tag()); c.Add(new Faq()); }); DbContextFake.SeedDb <IntranetApiContext>(c => { c.Add(new FaqTag { Faq = c.Faqs.SingleOrDefault(), Tag = c.Tags.SingleOrDefault() }); }, ensureDeleted: false); using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Act var tag = context.Tags.SingleOrDefault(); await context .Entry(tag) .Collection(c => c.FaqTags) .LoadAsync(); result = await TagHelpers.HasNoRelatedEntities(tag, context, tag.FaqTags); } using (var context = DbContextFake.GetDbContext <IntranetApiContext>()) { // Assert Assert.True(result); } }