예제 #1
0
        public void Setting_an_explicit_value_on_the_entity_marks_property_as_not_temporary()
        {
            using (var context = new KClrContext())
            {
                var entry       = context.Entry(new SomeEntity()).GetInfrastructure();
                var keyProperty = entry.EntityType.FindProperty("Id");

                var entity = (SomeEntity)entry.Entity;

                entry.SetEntityState(EntityState.Added);
                entry.SetTemporaryValue(keyProperty, -1);

                Assert.True(entry.HasTemporaryValue(keyProperty));

                entity.Id = 77;

                context.GetService <IChangeDetector>().DetectChanges(entry);

                Assert.False(entry.HasTemporaryValue(keyProperty));

                entry.SetEntityState(EntityState.Unchanged); // Does not throw

                var nameProperty = entry.EntityType.FindProperty(nameof(SomeEntity.Name));
                Assert.True(entry.HasDefaultValue(nameProperty));

                entity.Name = "Name";

                Assert.False(entry.HasDefaultValue(nameProperty));
            }
        }
예제 #2
0
        public void AcceptChanges_handles_different_entity_states_for_owned_types(EntityState entityState)
        {
            using (var context = new KClrContext())
            {
                var ownerEntry = context.Entry(
                    new OwnerClass
                {
                    Id    = 1,
                    Owned = new OwnedClass
                    {
                        Value = "Kool"
                    }
                }).GetInfrastructure();

                var entry         = context.Entry(((OwnerClass)ownerEntry.Entity).Owned).GetInfrastructure();
                var valueProperty = entry.EntityType.FindProperty(nameof(OwnedClass.Value));

                entry.SetEntityState(entityState);

                if (entityState != EntityState.Unchanged)
                {
                    entry[valueProperty] = "Pickle";
                }

                entry.SetOriginalValue(valueProperty, "Cheese");

                entry.AcceptChanges();

                Assert.Equal(
                    entityState == EntityState.Deleted || entityState == EntityState.Detached
                        ? EntityState.Detached
                        : EntityState.Unchanged,
                    entry.EntityState);
                if (entityState == EntityState.Unchanged)
                {
                    Assert.Equal("Kool", entry[valueProperty]);
                    Assert.Equal("Kool", entry.GetOriginalValue(valueProperty));
                }
                else
                {
                    Assert.Equal("Pickle", entry[valueProperty]);
                    Assert.Equal(
                        entityState == EntityState.Detached || entityState == EntityState.Deleted ? "Cheese" : "Pickle",
                        entry.GetOriginalValue(valueProperty));
                }
            }
        }