public void AddWithExistingKey() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; context.Values.Add(value); var value2 = new MyValue { Id = 1, Value = "2" }; Assert.IsFalse(context.Values.Add(value2)); } }
public void DeleteNonTracked() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Delete(value)); Assert.AreEqual(1, context.Values.Local.Count()); Assert.IsFalse(context.Values.Add(value)); Assert.AreEqual(1, context.Values.Local.Count()); } }
public void ChangeKeyResultsInException() { using (var context = new MyContext()) { context.ChangeTracker.AutoDetectChangesEnabled = false; var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Add(value)); Assert.IsTrue(context.ChangeTracker.HasChanges()); value.Id = 2; //next should throw exception context.ChangeTracker.DetectChanges(); } }
public void ContextDisableTrackingClearsTrackedEntries() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Add(value)); Assert.IsTrue(context.ChangeTracker.HasChanges()); context.TrackChanges = false; Assert.IsFalse(context.ChangeTracker.HasChanges()); Assert.AreEqual(0, context.ChangeTracker.Entries<MyValue>().Count()); Assert.AreEqual(0, context.ChangeTracker.Entries().Count()); } }
public void NoTrackingContextAddAttachDeleteDetachEntity() { using (var context = new MyContext()) { context.TrackChanges = false; var value = new MyValue { Id = 1, Value = "1" }; Assert.IsFalse(context.Values.Attach(value)); Assert.IsFalse(context.Values.Add(value)); Assert.IsFalse(context.Values.Delete(value)); Assert.IsFalse(context.Values.Detach(value)); Assert.IsFalse(context.ChangeTracker.HasChanges()); Assert.AreEqual(0, context.Values.Local.Count()); } }
public void AttachDetach() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; Assert.IsTrue(context.Values.Attach(value)); Assert.IsFalse(context.Values.Attach(value)); Assert.IsTrue(context.Values.Detach(value)); Assert.IsFalse(context.Values.Detach(value)); Assert.IsFalse(context.ChangeTracker.HasChanges()); Assert.AreEqual(0, context.Values.Local.Count()); } }
public void AddThenAttach() { using (var context = new MyContext()) { var value = new MyValue { Id = 1, Value = "1" }; context.Values.Add(value); Assert.IsFalse(context.Values.Attach(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 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); } }