public void AddNewEntity() { int count = 0; using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => { count++; Debug.WriteLine("EXECUTE QUERY: " + cql); }; var newValue = new MyValue { Id = 20000, Value = "Hallo 20000" }; bool added = context.Values.Add(newValue); Assert.IsTrue(added); context.SaveChanges(); //try save again (should do nothing) context.SaveChanges(); Assert.AreEqual(1, count, "Save again introduces new query!"); //try find (should do nothing) var entity = context.Values.Find(20000); Assert.AreSame(newValue, entity); Assert.AreEqual(1, count, "Find introduces new query!"); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(20000); Assert.IsNotNull(value); Assert.AreEqual("Hallo 20000", value.Value); } }
public void AddAndChangeNewEntity() { int count = 0; using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => { count++; Debug.WriteLine("EXECUTE QUERY: " + cql); }; var newValue = new MyValue { Id = 30000, Value = "Hallo 30000" }; bool added = context.Values.Add(newValue); Assert.IsTrue(added); IEntityEntry entry = context.ChangeTracker.Entry(newValue); Assert.IsNotNull(entry); Assert.AreEqual(EntityState.Added, entry.State); context.SaveChanges(); Assert.AreEqual(EntityState.Unchanged, entry.State); newValue.Value = "Hallo weer!"; context.SaveChanges(); Assert.AreEqual(2, count, "Where is my query?"); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(30000); Assert.IsNotNull(value); Assert.AreEqual("Hallo weer!", value.Value); } }
public void SelectAndUpdate() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var query = context.Values.Where(r => new[] { 201, 202, 203, 204 }.Contains(r.Id)).ToList(); query[1].Value = "Zo gaan we weer verder"; context.SaveChanges(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(202); Assert.IsNotNull(value); Assert.AreEqual("Zo gaan we weer verder", value.Value); } }
public void UpdateInExternalTransaction() { using (var connection = new CqlConnection(ConnectionString)) using (var transaction = connection.BeginTransaction()) using (var context = new MyContext(connection, false)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); context.Database.UseTransaction(transaction); var value = context.Values.Find(500); value.Value = "Hallo daar!"; context.SaveChanges(false); var command = new CqlCommand(connection, "update myvalue set value='adjusted' where id=500"); command.Transaction = transaction; command.ExecuteNonQuery(); transaction.Commit(); //accept all changes only after commit context.AcceptAllChanges(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(500); Assert.IsNotNull(value); Assert.AreEqual("adjusted", value.Value); } }
public void UpdateTwiceInTwoTransactions() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var value = context.Values.Find(400); using (var transaction1 = context.Database.BeginTransaction()) { value.Value = "Hallo daar!"; context.SaveChanges(); transaction1.Commit(); } using (var transaction2 = context.Database.BeginTransaction()) { transaction2.BatchType = CqlBatchType.Unlogged; value.Value = "Nog een keer"; context.SaveChanges(); transaction2.Commit(); } } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(400); Assert.IsNotNull(value); Assert.AreEqual("Nog een keer", value.Value); } }
public void UpdateTwiceInSingleTransactionAndRollback() { using (var context = new MyContext(ConnectionString)) using (var transaction = context.Database.BeginTransaction()) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var value = context.Values.Find(300); value.Value = "Hallo daar!"; context.SaveChanges(); value.Value = "Oops..."; context.SaveChanges(); transaction.Rollback(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(300); Assert.IsNotNull(value); Assert.AreEqual("Hallo 300", value.Value); } }
public void FindAndUpdate() { using (var context = new MyContext(ConnectionString)) { context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql); var value = context.Values.Find(100); value.Value = "Hallo daar!"; context.SaveChanges(); } using (var context = new MyContext(ConnectionString)) { var value = context.Values.Find(100); Assert.IsNotNull(value); Assert.AreEqual("Hallo daar!", value.Value); } }