public void DeleteFriendUsingGenericRepo() { using (var context = new PluralSightBookContext()) { var initializer = new TestDbInitializer(); initializer.Reseed(context); int friendCount = context.Friends.Count(); var testUserId = context.aspnet_Users.FirstOrDefault().UserId; var repo = new Repositories.Repository <Friend>(context); var friend = new Friend() { UserId = testUserId, EmailAddress = "*****@*****.**" }; repo.Add(friend); repo.Save(); // Hence always use the same context while saving and deleting the same object/entity // Fails because we're using a different object context //var repo2 = new Repositories.Repository<Friend>(); var repo2 = new Repositories.Repository <Friend>(context); repo2.Remove(friend); repo2.Save(); Assert.AreEqual(friendCount, context.Friends.Count()); } }
public void DeleteFriendUsingGenericRepoAndIoC() { var context = _container.TryGetInstance<DbContext>() as PluralSightBookContext; var initializer = new TestDbInitializer(); initializer.Reseed(context); int friendCount = context.Friends.Count(); var testUserId = context.Users .FirstOrDefault(u => u.UserName == TestDbInitializer.TEST_USERNAME) .UserId; var repo = new Repositories.Repository<Friend>(context); var friend = new Friend() { UserId = testUserId, EmailAddress = "*****@*****.**" }; repo.Add(friend); repo.Save(); var anotherContext = _container.TryGetInstance<DbContext>() as PluralSightBookContext; var repo2 = new Repositories.Repository<Friend>(anotherContext); repo2.Remove(friend); repo2.Save(); Assert.AreEqual(friendCount, context.Friends.Count()); }
public void AddFriendUsingGenericRepo() { using (var context = new PluralSightBookContext()) { var initializer = new TestDbInitializer(); initializer.Reseed(context); int friendCount = context.Friends.Count(); var testUserId = context.aspnet_Users .FirstOrDefault(u => u.UserName == TestDbInitializer.TEST_USERNAME) .UserId; var repo = new Repositories.Repository <Friend>(); repo.Add(new Friend() { UserId = testUserId, EmailAddress = "*****@*****.**" }); repo.Save(); Assert.AreEqual(friendCount + 1, context.Friends.Count()); } }