private string ChangeTagsBasedOnMultiSelectList(SampleWebAppDb db, Post post = null) { var requiredTagIds = UserChosenTags.GetFinalSelectionAsInts(); if (!requiredTagIds.Any()) { return("You must select at least one tag for the post."); } if (requiredTagIds.Any(x => db.Tags.Find(x) == null)) { return("Could not find one of the tags. Did another user delete it?"); } if (post != null) { //This is an update so we need to load the tags db.Entry(post).Collection(p => p.Tags).Load(); } var newTagsForPost = db.Tags.Where(x => requiredTagIds.Contains(x.TagId)).ToList(); Tags = newTagsForPost; //will be copied over by copyDtoToData return(null); }
public static async Task DeletePostEfDirectAsync(this SampleWebAppDb db, int postId) { var postClass = new Post { PostId = postId }; db.Entry(postClass).State = EntityState.Deleted; var status = await db.SaveChangesWithCheckingAsync(); status.IsValid.ShouldEqual(true, status.Errors); }
public void Check25UpdatePostToAddTagOk() { using (var db = new SampleWebAppDb()) { //SETUP var snap = new DbSnapShot(db); var badTag = db.Tags.Single(x => x.Slug == "bad"); var firstPost = db.Posts.First(); //ATTEMPT db.Entry(firstPost).Collection(x => x.Tags).Load(); firstPost.Tags.Add(badTag); var status = db.SaveChangesWithChecking(); //VERIFY status.IsValid.ShouldEqual(true, status.Errors); snap.CheckSnapShot(db, 0, 1); firstPost = db.Blogs.Include(x => x.Posts.Select(y => y.Tags)).First().Posts.First(); firstPost.Tags.Count.ShouldEqual(3); } }
public void Check26ReplaceTagsOk() { using (var db = new SampleWebAppDb()) { //SETUP var snap = new DbSnapShot(db); var firstPost = db.Posts.First(); var tagsNotInFirstPostTracked = db.Tags.Where(x => x.Posts.All(y => y.PostId != firstPost.PostId)).ToList(); //ATTEMPT db.Entry(firstPost).Collection(x => x.Tags).Load(); firstPost.Tags = tagsNotInFirstPostTracked; var status = db.SaveChangesWithChecking(); //VERIFY status.IsValid.ShouldEqual(true, status.Errors); snap.CheckSnapShot(db, 0, -1); firstPost = db.Posts.Include(x => x.Tags).First(); firstPost.Tags.Count.ShouldEqual(1); } }