class representing the values in a table
Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
        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());

            }
        }
Exemplo n.º 3
0
        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();
            }
        }
Exemplo n.º 4
0
        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());
            }
        }
Exemplo n.º 5
0
        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());
            }
        }
Exemplo n.º 6
0
        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());
            }
        }
Exemplo n.º 7
0
        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));
            }
        }
Exemplo n.º 8
0
        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);
            }
        }
Exemplo n.º 9
0
        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);
            }
        }