public void Can_obtain_context()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Same(context, context.Entry(entity).Context);
                Assert.Same(context, context.Entry((object)entity).Context);
            }
        }
        public void Can_obtain_entity_instance()
        {
            using (var context = new FreezerContext())
            {
                var entity = new Chunky();
                context.Add(entity);

                Assert.Same(entity, context.Entry(entity).Entity);
                Assert.Same(entity, context.Entry((object)entity).Entity);
            }
        }
        public void Can_obtain_underlying_state_entry()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;
                var entry = ((IAccessor<IStateManager>)context.ChangeTracker).Service.GetOrCreateEntry(entity);

                Assert.Same(entry, ((IAccessor<InternalEntityEntry>)context.Entry(entity)).Service);
                Assert.Same(entry, ((IAccessor<InternalEntityEntry>)context.Entry((object)entity)).Service);
            }
        }
        public void Can_get_metadata()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;
                var entityType = context.Model.GetEntityType(typeof(Chunky));

                Assert.Same(entityType, context.Entry(entity).Metadata);
                Assert.Same(entityType, context.Entry((object)entity).Metadata);
            }
        }
        public void Can_obtain_underlying_state_entry()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;
                var entry = context.ChangeTracker.GetService().GetOrCreateEntry(entity);

                Assert.Same(entry, context.Entry(entity).GetService());
                Assert.Same(entry, context.Entry((object)entity).GetService());
            }
        }
        public void Can_get_and_change_state()
        {
            using (var context = new FreezerContext())
            {
                var entity = new Chunky();
                var entry = ((IAccessor<InternalEntityEntry>)context.Add(entity)).Service;

                context.Entry(entity).State = EntityState.Modified;
                Assert.Equal(EntityState.Modified, entry.EntityState);
                Assert.Equal(EntityState.Modified, context.Entry(entity).State);

                context.Entry((object)entity).State = EntityState.Unchanged;
                Assert.Equal(EntityState.Unchanged, entry.EntityState);
                Assert.Equal(EntityState.Unchanged, context.Entry((object)entity).State);
            }
        }
        public void Can_get_and_change_state()
        {
            using (var context = new FreezerContext())
            {
                var entity = new Chunky();
                var entry = context.Add(entity).GetInfrastructure();

                context.Entry(entity).State = EntityState.Modified;
                Assert.Equal(EntityState.Modified, entry.EntityState);
                Assert.Equal(EntityState.Modified, context.Entry(entity).State);

                context.Entry((object)entity).State = EntityState.Unchanged;
                Assert.Equal(EntityState.Unchanged, entry.EntityState);
                Assert.Equal(EntityState.Unchanged, context.Entry((object)entity).State);
            }
        }
Beispiel #8
0
        public void Throws_when_wrong_generic_type_is_used_while_getting_property_entry_by_name()
        {
            using var context = new FreezerContext();
            var entity = context.Add(new Chunky()).Entity;

            Assert.Equal(
                CoreStrings.WrongGenericPropertyType("Monkey", entity.GetType().ShortDisplayName(), "int", "string"),
                Assert.Throws <ArgumentException>(() => context.Entry(entity).Property <string>("Monkey")).Message);
        }
Beispiel #9
0
        public async Task Can_get_and_change_state_async()
        {
            using (var context = new FreezerContext())
            {
                var entity     = new Chunky();
                var stateEntry = context.Add(entity).StateEntry;

                await context.Entry(entity).SetStateAsync(EntityState.Modified);

                Assert.Equal(EntityState.Modified, stateEntry.EntityState);
                Assert.Equal(EntityState.Modified, context.Entry(entity).State);

                await context.Entry((object)entity).SetStateAsync(EntityState.Unchanged);

                Assert.Equal(EntityState.Unchanged, stateEntry.EntityState);
                Assert.Equal(EntityState.Unchanged, context.Entry((object)entity).State);
            }
        }
        public void Throws_when_accessing_property_as_navigation()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal(
                    CoreStrings.NavigationIsProperty(
                        "Monkey", entity.GetType().Name,
                        nameof(EntityEntry.Reference), nameof(EntityEntry.Collection), nameof(EntityEntry.Property)),
                    Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Navigation("Monkey").Metadata.Name).Message);
                Assert.Equal(
                    CoreStrings.NavigationIsProperty(
                        "Monkey", entity.GetType().Name,
                        nameof(EntityEntry.Reference), nameof(EntityEntry.Collection), nameof(EntityEntry.Property)),
                    Assert.Throws <InvalidOperationException>(() => context.Entry((object)entity).Navigation("Monkey").Metadata.Name).Message);
            }
        }
Beispiel #11
0
        public void Can_get_metadata_generic()
        {
            using var context = new FreezerContext();
            var entity = new Chunky();

            context.Add(entity);

            Assert.Equal("Garcia", context.Entry(entity).Reference(e => e.Garcia).Metadata.Name);
        }
Beispiel #12
0
        public void Can_get_metadata_reference()
        {
            using var context = new FreezerContext();
            var entity = new Chunky();

            context.Add(entity);

            Assert.Equal("Garcia", context.Entry(entity).Navigation("Garcia").Metadata.Name);
        }
Beispiel #13
0
        public void Can_get_back_reference_generic()
        {
            using var context = new FreezerContext();
            var entity = new Cherry();
            context.Add(entity);

            var entityEntry = context.Entry(entity);
            Assert.Same(entityEntry.Entity, entityEntry.Collection(e => e.Monkeys).EntityEntry.Entity);
        }
        public void Can_get_generic_property_entry_by_lambda()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal("Monkey", context.Entry(entity).Property(e => e.Monkey).Metadata.Name);
            }
        }
Beispiel #15
0
        public void Can_get_metadata_property()
        {
            using var context = new FreezerContext();
            var entity = new Chunky();

            context.Add(entity);

            Assert.Equal("Monkey", context.Entry(entity).Member("Monkey").Metadata.Name);
        }
        public void Can_get_generic_collection_entry_by_name()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Cherry()).Entity;

                Assert.Equal("Monkeys", context.Entry(entity).Collection <Chunky>("Monkeys").Metadata.Name);
            }
        }
Beispiel #17
0
        public void Can_get_metadata_collection()
        {
            using var context = new FreezerContext();
            var entity = new Cherry();

            context.Add(entity);

            Assert.Equal("Monkeys", context.Entry(entity).Navigation("Monkeys").Metadata.Name);
        }
        public void Can_get_generic_reference_entry_by_lambda()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal("Garcia", context.Entry(entity).Reference(e => e.Garcia).Metadata.Name);
            }
        }
    public void IsModified_can_set_fk_to_modified_principal_with_Modified_dependents(
        EntityState principalState,
        EntityState dependentState)
    {
        using var context = new FreezerContext();
        var cherry = new Cherry {
            Id = 1
        };
        var chunky1 = new Chunky {
            Id = 1, Garcia = cherry
        };
        var chunky2 = new Chunky {
            Id = 2, Garcia = cherry
        };

        cherry.Monkeys = new List <Chunky> {
            chunky1, chunky2
        };

        context.Attach(chunky1);
        context.Attach(chunky2);

        context.Entry(cherry).State  = principalState;
        context.Entry(chunky1).State = dependentState;
        context.Entry(chunky2).State = dependentState;

        var collection = context.Entry(cherry).Collection(e => e.Monkeys);

        Assert.True(collection.IsModified);

        collection.IsModified = false;

        Assert.False(collection.IsModified);
        Assert.False(context.Entry(chunky1).Property(e => e.GarciaId).IsModified);
        Assert.False(context.Entry(chunky2).Property(e => e.GarciaId).IsModified);

        collection.IsModified = true;

        Assert.True(collection.IsModified);
        Assert.True(context.Entry(chunky1).Property(e => e.GarciaId).IsModified);
        Assert.True(context.Entry(chunky2).Property(e => e.GarciaId).IsModified);
        Assert.Equal(dependentState, context.Entry(chunky1).State);
        Assert.Equal(dependentState, context.Entry(chunky2).State);
    }
Beispiel #20
0
        public void Throws_when_wrong_generic_type_is_used_while_getting_property_entry_by_name()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal(Strings.WrongGenericPropertyType("Monkey", entity.GetType(), typeof(int).Name, typeof(string).Name),
                             Assert.Throws <ArgumentException>(() => context.Entry(entity).Property <string>("Monkey")).Message);
            }
        }
Beispiel #21
0
        public void Can_get_metadata_generic()
        {
            using (var context = new FreezerContext())
            {
                var entity = new Cherry();
                context.Add(entity);

                Assert.Equal("Monkeys", context.Entry(entity).Collection(e => e.Monkeys).Metadata.Name);
            }
        }
Beispiel #22
0
        public void Throws_when_wrong_collection_name_is_used_while_getting_property_entry_by_name()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Cherry()).Entity;

                Assert.Equal(
                    CoreStrings.PropertyNotFound("Chimp", entity.GetType().Name),
                    Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Collection("Chimp").Metadata.Name).Message);
                Assert.Equal(
                    CoreStrings.PropertyNotFound("Chimp", entity.GetType().Name),
                    Assert.Throws <InvalidOperationException>(() => context.Entry((object)entity).Collection("Chimp").Metadata.Name)
                    .Message);
                Assert.Equal(
                    CoreStrings.PropertyNotFound("Chimp", entity.GetType().Name),
                    Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Collection <Cherry>("Chimp").Metadata.Name)
                    .Message);
            }
        }
Beispiel #23
0
        public void Can_get_metadata()
        {
            using (var context = new FreezerContext())
            {
                var entity = new Chunky();
                context.Add(entity);

                Assert.Equal("Garcia", context.Entry(entity).Reference("Garcia").Metadata.Name);
            }
        }
        public void Can_get_back_reference_collection()
        {
            using var context = new FreezerContext();
            var entity = new Cherry();

            context.Add(entity);

            var entityEntry = context.Entry(entity);

            Assert.Same(entityEntry.Entity, entityEntry.Member("Chunkies").EntityEntry.Entity);
        }
        public void Can_get_back_reference_collection()
        {
            using (var context = new FreezerContext())
            {
                var entity = new Cherry();
                context.Add(entity);

                var entityEntry = context.Entry(entity);
                Assert.Same(entityEntry.Entity, entityEntry.Navigation("Monkeys").EntityEntry.Entity);
            }
        }
Beispiel #26
0
        public void Can_get_back_reference_property()
        {
            using var context = new FreezerContext();
            var entity = new Chunky();

            context.Add(entity);

            var entityEntry = context.Entry(entity);

            Assert.Same(entityEntry.Entity, entityEntry.Member("Monkey").EntityEntry.Entity);
        }
        public void Throws_when_accessing_collection_as_reference()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Cherry()).Entity;

                Assert.Equal(CoreStrings.ReferenceIsCollection("Monkeys", entity.GetType().Name,
                                                               nameof(EntityEntry.Reference), nameof(EntityEntry.Collection)),
                             Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Reference("Monkeys").Metadata.Name).Message);
                Assert.Equal(CoreStrings.ReferenceIsCollection("Monkeys", entity.GetType().Name,
                                                               nameof(EntityEntry.Reference), nameof(EntityEntry.Collection)),
                             Assert.Throws <InvalidOperationException>(() => context.Entry((object)entity).Reference("Monkeys").Metadata.Name).Message);
                Assert.Equal(CoreStrings.ReferenceIsCollection("Monkeys", entity.GetType().Name,
                                                               nameof(EntityEntry.Reference), nameof(EntityEntry.Collection)),
                             Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Reference <Random>("Monkeys").Metadata.Name).Message);
                Assert.Equal(CoreStrings.ReferenceIsCollection("Monkeys", entity.GetType().Name,
                                                               nameof(EntityEntry.Reference), nameof(EntityEntry.Collection)),
                             Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Reference(e => e.Monkeys).Metadata.Name).Message);
            }
        }
        public void Can_get_back_reference_reference()
        {
            using (var context = new FreezerContext())
            {
                var entity = new Chunky();
                context.Add(entity);

                var entityEntry = context.Entry(entity);
                Assert.Same(entityEntry.Entity, entityEntry.Navigation("Garcia").EntityEntry.Entity);
            }
        }
Beispiel #29
0
        public void Can_get_back_reference_generic()
        {
            using var context = new FreezerContext();
            var entity = new Chunky();

            context.Add(entity);

            var entityEntry = context.Entry(entity);

            Assert.Same(entityEntry.Entity, entityEntry.Reference(e => e.Garcia).EntityEntry.Entity);
        }
Beispiel #30
0
        public void IsModified_tracks_state_of_FK_property_principal()
        {
            using var context = new FreezerContext();
            var cherry = new Cherry();
            var chunky1 = new Chunky { Id = 1, Garcia = cherry };
            var chunky2 = new Chunky { Id = 2, Garcia = cherry };
            cherry.Monkeys = new List<Chunky> { chunky1, chunky2 };
            context.AttachRange(cherry, chunky1, chunky2);

            var collection = context.Entry(cherry).Collection(e => e.Monkeys);

            Assert.False(collection.IsModified);

            context.Entry(chunky1).State = EntityState.Modified;

            Assert.True(collection.IsModified);

            context.Entry(chunky1).State = EntityState.Unchanged;

            Assert.False(collection.IsModified);
        }
Beispiel #31
0
        public void Can_get_and_set_current_value_start_tracking_generic()
        {
            using var context = new FreezerContext();
            var cherry = new Cherry();
            var chunky = new Chunky();

            context.Add(chunky);

            var reference = context.Entry(chunky).Reference(e => e.Garcia);

            Assert.Null(reference.CurrentValue);

            reference.CurrentValue = cherry;

            Assert.Same(cherry, chunky.Garcia);
            Assert.Same(chunky, cherry.Monkeys.Single());
            Assert.Equal(cherry.Id, chunky.GarciaId);
            Assert.Same(cherry, reference.CurrentValue);

            Assert.Same(reference.TargetEntry.GetInfrastructure(), context.Entry(cherry).GetInfrastructure());
            Assert.Equal(EntityState.Added, context.Entry(cherry).State);
            Assert.Equal(EntityState.Added, context.Entry(chunky).State);

            reference.CurrentValue = null;

            Assert.Null(chunky.Garcia);
            Assert.Empty(cherry.Monkeys);
            Assert.Null(chunky.GarciaId);
            Assert.Null(reference.CurrentValue);

            Assert.Null(reference.TargetEntry);
            Assert.Equal(EntityState.Added, context.Entry(cherry).State);
            Assert.Equal(EntityState.Added, context.Entry(chunky).State);
        }
Beispiel #32
0
        public void IsModified_can_set_fk_to_modified_principal_with_Modified_dependent(
            EntityState principalState,
            EntityState dependentState)
        {
            using var context = new FreezerContext();
            var half = new Half {
                Id = 7
            };
            var chunky = new Chunky {
                Id = 1, Baked = half
            };

            half.Monkey = chunky;

            context.Attach(half);

            context.Entry(chunky).State = principalState;
            context.Entry(half).State   = dependentState;

            var reference = context.Entry(chunky).Reference(e => e.Baked);

            Assert.True(reference.IsModified);

            reference.IsModified = false;

            Assert.False(reference.IsModified);
            Assert.False(context.Entry(half).Property(e => e.MonkeyId).IsModified);

            reference.IsModified = true;

            Assert.True(reference.IsModified);
            Assert.True(context.Entry(half).Property(e => e.MonkeyId).IsModified);
            Assert.Equal(dependentState, context.Entry(half).State);
        }
Beispiel #33
0
        public void Throws_when_accessing_reference_as_collection()
        {
            using var context = new FreezerContext();
            var entity = context.Add(new Chunky()).Entity;

            Assert.Equal(
                CoreStrings.CollectionIsReference(
                    "Garcia", entity.GetType().Name,
                    nameof(EntityEntry.Collection), nameof(EntityEntry.Reference)),
                Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Collection("Garcia").Metadata.Name).Message);
            Assert.Equal(
                CoreStrings.CollectionIsReference(
                    "Garcia", entity.GetType().Name,
                    nameof(EntityEntry.Collection), nameof(EntityEntry.Reference)),
                Assert.Throws <InvalidOperationException>(() => context.Entry((object)entity).Collection("Garcia").Metadata.Name)
                .Message);
            Assert.Equal(
                CoreStrings.CollectionIsReference(
                    "Garcia", entity.GetType().Name,
                    nameof(EntityEntry.Collection), nameof(EntityEntry.Reference)),
                Assert.Throws <InvalidOperationException>(() => context.Entry(entity).Collection <Cherry>("Garcia").Metadata.Name)
                .Message);
        }
Beispiel #34
0
    public void Can_get_all_modified_properties()
    {
        using var context = new FreezerContext();
        var entity = context.Attach(new Chunky()).Entity;

        var modified = context.Entry(entity).Properties
                       .Where(e => e.IsModified).Select(e => e.Metadata.Name).ToList();

        Assert.Empty(modified);

        entity.Nonkey   = "Blue";
        entity.GarciaId = 77;

        context.ChangeTracker.DetectChanges();

        modified = context.Entry(entity).Properties
                   .Where(e => e.IsModified).Select(e => e.Metadata.Name).ToList();

        Assert.Equal(
            new List <string> {
            "GarciaId", "Nonkey"
        }, modified);
    }
Beispiel #35
0
    public void Detached_entities_are_not_returned_from_the_change_tracker()
    {
        using var context = new FreezerContext();
        var entity = new Chunky {
            Id = 808
        };

        context.Attach(entity);

        Assert.Single(context.ChangeTracker.Entries());

        context.Entry(entity).State = EntityState.Detached;

        Assert.Empty(context.ChangeTracker.Entries());

        context.ChangeTracker.DetectChanges();

        Assert.Empty(context.ChangeTracker.Entries());

        context.Entry(entity);

        Assert.Empty(context.ChangeTracker.Entries());
    }
Beispiel #36
0
        public void IsModified_tracks_state_of_FK_property_principal()
        {
            using var context = new FreezerContext();
            var half   = new Half();
            var chunky = new Chunky {
                Baked = half
            };

            half.Monkey = chunky;
            context.AttachRange(chunky, half);

            var reference = context.Entry(chunky).Reference(e => e.Baked);

            Assert.False(reference.IsModified);

            context.Entry(half).State = EntityState.Modified;

            Assert.True(reference.IsModified);

            context.Entry(half).State = EntityState.Unchanged;

            Assert.False(reference.IsModified);
        }
Beispiel #37
0
        public void Can_get_and_set_current_value_start_tracking_generic()
        {
            using (var context = new FreezerContext())
            {
                var cherry = new Cherry();
                var chunky = new Chunky();
                context.Add(cherry);

                var collection = context.Entry(cherry).Collection(e => e.Monkeys);

                Assert.Null(collection.CurrentValue);

                collection.CurrentValue = new List <Chunky>
                {
                    chunky
                };

                Assert.Same(cherry, chunky.Garcia);
                Assert.Same(chunky, cherry.Monkeys.Single());
                Assert.Equal(cherry.Id, chunky.GarciaId);
                Assert.Same(chunky, collection.CurrentValue.Single());

                Assert.Equal(EntityState.Added, context.Entry(cherry).State);
                Assert.Equal(EntityState.Added, context.Entry(chunky).State);

                collection.CurrentValue = null;

                Assert.Null(chunky.Garcia);
                Assert.Null(cherry.Monkeys);
                Assert.Null(chunky.GarciaId);
                Assert.Null(collection.CurrentValue);

                Assert.Equal(EntityState.Added, context.Entry(cherry).State);
                Assert.Equal(EntityState.Added, context.Entry(chunky).State);
            }
        }
        public void Can_get_generic_property_entry_by_lambda()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal("Monkey", context.Entry(entity).Property(e => e.Monkey).Metadata.Name);
            }
        }
        public void Throws_when_wrong_generic_type_is_used_while_getting_property_entry_by_name()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal(Strings.WrongGenericPropertyType("Monkey", entity.GetType(), typeof(int).Name, typeof(string).Name),
                    Assert.Throws<ArgumentException>(() => context.Entry(entity).Property<string>("Monkey")).Message);
            }
        }
        public void Can_get_property_entry_by_name()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal("Monkey", context.Entry(entity).Property("Monkey").Metadata.Name);
                Assert.Equal("Monkey", context.Entry((object)entity).Property("Monkey").Metadata.Name);
            }
        }
        public void Throws_when_wrong_property_name_is_used_while_getting_property_entry_by_name()
        {
            using (var context = new FreezerContext())
            {
                var entity = context.Add(new Chunky()).Entity;

                Assert.Equal(Strings.PropertyNotFound("Chimp", entity.GetType()),
                    Assert.Throws<ModelItemNotFoundException>(() => context.Entry(entity).Property("Chimp").Metadata.Name).Message);
                Assert.Equal(Strings.PropertyNotFound("Chimp", entity.GetType()),
                    Assert.Throws<ModelItemNotFoundException>(() => context.Entry((object)entity).Property("Chimp").Metadata.Name).Message);
                Assert.Equal(Strings.PropertyNotFound("Chimp", entity.GetType()),
                    Assert.Throws<ModelItemNotFoundException>(() => context.Entry(entity).Property<int>("Chimp").Metadata.Name).Message);
            }
        }