Exemplo n.º 1
0
        public void Entity_PropertyChanged()
        {
            TestCityContainer ec = new TestCityContainer();
            var  el   = ec.GetEntitySet <City>();
            City city = new City
            {
                Name       = "Perrysburg",
                CountyName = "Wood",
                StateName  = "OH"
            };

            el.Attach(city);

            int hasChangesChangeCount                = 0;
            int entityStateChangeCount               = 0;
            int validationErrorsChangeCount          = 0;
            int totalPropertyChangeNotificationCount = 0;

            city.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                totalPropertyChangeNotificationCount++;

                if (e.PropertyName == "HasChanges")
                {
                    hasChangesChangeCount++;
                }
                else if (e.PropertyName == "EntityState")
                {
                    entityStateChangeCount++;
                }
                else if (e.PropertyName == "ValidationErrors")
                {
                    validationErrorsChangeCount++;
                }
            };
            Assert.IsFalse(city.HasChanges);
            Assert.IsFalse(((IChangeTracking)city).IsChanged);
            city.StateName = "WA";
            Assert.IsTrue(city.HasChanges);
            Assert.IsTrue(((IChangeTracking)city).IsChanged);
            ((IChangeTracking)city).AcceptChanges();
            Assert.IsFalse(city.HasChanges);
            Assert.IsFalse(((IChangeTracking)city).IsChanged);
            city.AssignCityZone("Foo");
            Assert.IsTrue(city.HasChanges);
            Assert.IsTrue(((IChangeTracking)city).IsChanged);
            ((IChangeTracking)city).AcceptChanges();
            el.Remove(city);
            Assert.AreEqual(4, hasChangesChangeCount);
            Assert.AreEqual(5, entityStateChangeCount);

            Assert.AreEqual(0, validationErrorsChangeCount);
            city.ValidationResultCollection.ReplaceErrors(new ValidationResult[] { new ValidationResult(string.Empty) });
            Assert.AreEqual(1, validationErrorsChangeCount);
            city.ValidationErrors.Clear();
            Assert.AreEqual(2, validationErrorsChangeCount);

            Assert.AreEqual(23, totalPropertyChangeNotificationCount);
        }
Exemplo n.º 2
0
        public void Entity_AcceptChanges()
        {
            TestCityContainer ec = new TestCityContainer();
            var el = ec.GetEntitySet <City>();

            string name       = "Perrysburg";
            string countyName = "Wood";
            string stateName  = "OH";

            object[] key = new object[] { countyName, name, stateName };

            City city = new City
            {
                Name       = name,
                CountyName = countyName,
                StateName  = stateName
            };

            el.Attach(city);
            Assert.AreEqual(1, el.Count);

            var cityInCache = el.GetEntityByKey(key);

            Assert.AreEqual(city, cityInCache);

            // Simulate a delete.
            el.Remove(city);
            Assert.AreEqual(0, el.Count);
            Assert.AreEqual(EntityState.Deleted, city.EntityState);

            // Verify that it's still in the cache.
            cityInCache = el.GetEntityByKey(key);
            Assert.AreEqual(city, cityInCache);

            // Clear out part of the PK. (This could happen if someone removes an entity via an EntityCollection. See bug 619454.)
            city.CountyName = "";

            ((IChangeTracking)city).AcceptChanges();
            Assert.AreEqual(EntityState.Detached, city.EntityState);

            // Verify it's removed from the cache now.
            cityInCache = el.GetEntityByKey(key);
            Assert.IsNull(cityInCache);
        }
Exemplo n.º 3
0
        public void Entity_GetOriginal()
        {
            TestCityContainer ec = new TestCityContainer();
            var  el   = ec.GetEntitySet <City>();
            City city = new City
            {
                Name       = "Perrysburg",
                CountyName = "Wood",
                StateName  = "OH",
                ZoneName   = "Foo"
            };

            el.Attach(city);

            // There should not be any original state right now, so this should return null.
            var original = (City)city.GetOriginal();

            Assert.IsNull(original);

            city.ZoneName = "Bar"; // Make a change, causing entity to create original state.
            original      = (City)city.GetOriginal();
            Assert.AreEqual("Foo", original.ZoneName);
        }