Beispiel #1
0
        public void Can_get_and_set_current_value_generic()
        {
            using var context = new FreezerContext();
            var cherry = new Cherry();
            var chunky = new Chunky();

            context.AddRange(chunky, cherry);

            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());

            reference.CurrentValue = null;

            Assert.Null(chunky.Garcia);
            Assert.Empty(cherry.Monkeys);
            Assert.Null(chunky.GarciaId);
            Assert.Null(reference.CurrentValue);
            Assert.Null(reference.TargetEntry);
        }
        public void Can_get_and_set_current_value_collection()
        {
            using (var context = new FreezerContext())
            {
                var cherry = new Cherry();
                var chunky = new Chunky();
                context.AddRange(chunky, cherry);

                var collection = context.Entry(cherry).Navigation("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, ((ICollection <Chunky>)collection.CurrentValue).Single());

                collection.CurrentValue = null;

                Assert.Null(chunky.Garcia);
                Assert.Null(cherry.Monkeys);
                Assert.Null(chunky.GarciaId);
                Assert.Null(collection.CurrentValue);
            }
        }
        public void Can_get_and_set_current_value_reference()
        {
            using (var context = new FreezerContext())
            {
                var cherry = new Cherry();
                var chunky = new Chunky();
                context.AddRange(chunky, cherry);

                var reference = context.Entry(chunky).Navigation("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);

                reference.CurrentValue = null;

                Assert.Null(chunky.Garcia);
                Assert.Empty(cherry.Monkeys);
                Assert.Null(chunky.GarciaId);
                Assert.Null(reference.CurrentValue);
            }
        }
Beispiel #4
0
        public void Can_get_and_set_current_value_generic()
        {
            using (var context = new FreezerContext())
            {
                var cherry = new Cherry();
                var chunky = new Chunky();
                context.AddRange(chunky, 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.Same(collection.GetTargetEntry(chunky).GetInfrastructure(), context.Entry(chunky).GetInfrastructure());

                collection.CurrentValue = null;

                Assert.Null(chunky.Garcia);
                Assert.Null(cherry.Monkeys);
                Assert.Null(chunky.GarciaId);
                Assert.Null(collection.CurrentValue);
                Assert.Null(collection.GetTargetEntry(chunky));
            }
        }
        public void Can_get_and_set_current_value_collection()
        {
            using var context = new FreezerContext();

            var cherry = new Cherry();
            var chunky = new Chunky();

            context.AddRange(chunky, cherry);

            var collection        = context.Entry(cherry).Member("Chunkies");
            var inverseCollection = context.Entry(chunky).Member("Cherries");

            Assert.Null(collection.CurrentValue);
            Assert.Null(inverseCollection.CurrentValue);

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

            Assert.Same(cherry, chunky.Cherries.Single());
            Assert.Same(chunky, cherry.Chunkies.Single());
            Assert.Equal(cherry, ((ICollection <Cherry>)inverseCollection.CurrentValue).Single());
            Assert.Same(chunky, ((ICollection <Chunky>)collection.CurrentValue).Single());

            collection.CurrentValue = null;

            Assert.Empty(chunky.Cherries);
            Assert.Null(cherry.Chunkies);
            Assert.Empty((IEnumerable)inverseCollection.CurrentValue);
            Assert.Null(collection.CurrentValue);
        }
Beispiel #6
0
        private static void AttachGraph(FreezerContext context, Cherry cherry1, Cherry cherry2, Chunky chunky1, Chunky chunky2)
        {
            cherry1.Chunkies = new List <Chunky> {
                chunky1, chunky2
            };
            cherry2.Chunkies = new List <Chunky>();

            if (context is ExplicitFreezerContext)
            {
                context.AddRange(cherry1, cherry2, chunky1, chunky2); // So that PKs get generated values
                context.ChangeTracker.Entries().ToList().ForEach(e => e.State = EntityState.Unchanged);
            }
            else
            {
                context.AttachRange(cherry1, cherry2, chunky1, chunky2);
            }
        }