コード例 #1
0
        private void SetPropertyClrTest <TEntity>(TEntity entity, bool needsDetectChanges)
            where TEntity : class, ISomeEntity
        {
            using (var context = new KClrContext())
            {
                var entry        = context.Attach(entity).GetInfrastructure();
                var nameProperty = entry.EntityType.FindProperty("Name");

                Assert.False(entry.IsModified(nameProperty));
                Assert.Equal(EntityState.Unchanged, entry.EntityState);

                entity.Name = "Kool";

                Assert.False(entry.IsModified(nameProperty));
                Assert.Equal(EntityState.Unchanged, entry.EntityState);

                entity.Name = "Beans";

                if (needsDetectChanges)
                {
                    Assert.False(entry.IsModified(nameProperty));
                    Assert.Equal(EntityState.Unchanged, entry.EntityState);

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

                Assert.True(entry.IsModified(nameProperty));
                Assert.Equal(EntityState.Modified, entry.EntityState);
            }
        }
コード例 #2
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));
            }
        }