public async Task Can_save_changes_in_tracked_entities() { using (var testDatabase = OracleTestStore.CreateInitialized(DatabaseName)) { int updatedId; int deletedId; int addedId; var options = Fixture.CreateOptions(testDatabase); using (var db = new BloggingContext(options)) { var blogs = await CreateBlogDatabaseAsync <Blog>(db); var toAdd = db.Blogs.Add( new Blog { Name = "Blog to Insert", George = true, TheGu = new Guid("0456AEF1-B7FC-47AA-8102-975D6BA3A9BF"), NotFigTime = new DateTime(1973, 9, 3, 0, 10, 33, 777), ToEat = 64, OrNothing = 0.123456789, Fuse = 777, WayRound = 9876543210, Away = 0.12345f, AndChew = new byte[16] }).Entity; db.Entry(toAdd).State = EntityState.Detached; var toUpdate = blogs[0]; toUpdate.Name = "Blog is Updated"; updatedId = toUpdate.Id; var toDelete = blogs[1]; toDelete.Name = "Blog to delete"; deletedId = toDelete.Id; db.Remove(toDelete); db.Entry(toAdd).State = EntityState.Added; await db.SaveChangesAsync(); addedId = toAdd.Id; Assert.NotEqual(0, addedId); Assert.Equal(EntityState.Unchanged, db.Entry(toUpdate).State); Assert.Equal(EntityState.Unchanged, db.Entry(toAdd).State); Assert.DoesNotContain(toDelete, db.ChangeTracker.Entries().Select(e => e.Entity)); } using (var db = new BloggingContext(options)) { var toUpdate = db.Blogs.Single(b => b.Id == updatedId); Assert.Equal("Blog is Updated", toUpdate.Name); Assert.Equal(0, db.Blogs.Count(b => b.Id == deletedId)); Assert.Equal("Blog to Insert", db.Blogs.Single(b => b.Id == addedId).Name); } } }
public async Task Can_save_changes() { using (var testDatabase = OracleTestStore.CreateInitialized(DatabaseName)) { var options = Fixture.CreateOptions(testDatabase); using (var db = new BloggingContext(options)) { await CreateBlogDatabaseAsync <Blog>(db); } Fixture.TestSqlLoggerFactory.Clear(); using (var db = new BloggingContext(options)) { var toUpdate = db.Blogs.Single(b => b.Name == "Blog1"); toUpdate.Name = "Blog is Updated"; var updatedId = toUpdate.Id; var toDelete = db.Blogs.Single(b => b.Name == "Blog2"); toDelete.Name = "Blog to delete"; var deletedId = toDelete.Id; db.Entry(toUpdate).State = EntityState.Modified; db.Entry(toDelete).State = EntityState.Deleted; var toAdd = db.Add( new Blog { Name = "Blog to Insert", George = true, TheGu = new Guid("0456AEF1-B7FC-47AA-8102-975D6BA3A9BF"), NotFigTime = new DateTime(1973, 9, 3, 0, 10, 33, 777), ToEat = 64, OrNothing = 0.123456789, Fuse = 777, WayRound = 9876543210, Away = 0.12345f, AndChew = new byte[16] }).Entity; await db.SaveChangesAsync(); var addedId = toAdd.Id; Assert.NotEqual(0, addedId); Assert.Equal(EntityState.Unchanged, db.Entry(toUpdate).State); Assert.Equal(EntityState.Unchanged, db.Entry(toAdd).State); Assert.DoesNotContain(toDelete, db.ChangeTracker.Entries().Select(e => e.Entity)); Assert.Equal(5, Fixture.TestSqlLoggerFactory.SqlStatements.Count); Assert.Contains("SELECT", Fixture.TestSqlLoggerFactory.SqlStatements[0]); Assert.Contains("SELECT", Fixture.TestSqlLoggerFactory.SqlStatements[1]); Assert.Contains(":p0='" + deletedId, Fixture.TestSqlLoggerFactory.SqlStatements[2]); Assert.Contains("DELETE", Fixture.TestSqlLoggerFactory.SqlStatements[2]); Assert.Contains("UPDATE", Fixture.TestSqlLoggerFactory.SqlStatements[3]); Assert.Contains("INSERT", Fixture.TestSqlLoggerFactory.SqlStatements[4]); var rows = await testDatabase.ExecuteScalarAsync <int>( $@"SELECT Count(*) FROM ""Blog"" WHERE ""Id"" = {updatedId} AND ""Name"" = 'Blog is Updated'"); Assert.Equal(1, rows); rows = await testDatabase.ExecuteScalarAsync <int>( $@"SELECT Count(*) FROM ""Blog"" WHERE ""Id"" = {deletedId}"); Assert.Equal(0, rows); rows = await testDatabase.ExecuteScalarAsync <int>( $@"SELECT Count(*) FROM ""Blog"" WHERE ""Id"" = {addedId} AND ""Name"" = 'Blog to Insert'"); Assert.Equal(1, rows); } } }