示例#1
0
        public void EFComprehendsMixedStatesWhenAddingUntrackedGraph()
        {
            var ninja = EF6Model.RichModels.Ninja.CreateIndependent("julie", true);

            ninja.SpecifyClan(new EF6Model.RichModels.Clan {
                Id = 1, ClanName = "Clan from database"
            });
            using (var context = new NinjaContext()) {
                context.Ninjas.Add(ninja);
                var entries = context.ChangeTracker.Entries();
                OutputState(entries);
                context.FixState();
                OutputState(entries);
                Assert.IsTrue(entries.Any(e => e.State == EntityState.Unchanged));
            }
        }
        public void EFComprehendsMixedStatesWhenAddingUntrackedGraph()
        {
            var ninja = EF6Model.RichModels.Ninja.CreateIndependent("julie", true);

            ninja.SpecifyClan(new EF6Model.RichModels.Clan {
                Id = 1, ClanName = "Clan from database"
            });
            using (var context = new NinjaContext()) {
                context.Ninjas.Add(ninja);
                var entries = context.ChangeTracker.Entries();
                entries.ToList().ForEach(e => Debug.WriteLine($"Before:{e.Entity.ToString()}:{e.State}"));
                context.FixState();
                entries.ToList().ForEach(e => Debug.WriteLine($"After:{e.Entity.ToString()}:{e.State}"));
                Assert.IsTrue(entries.Any(e => e.State == EntityState.Unchanged));
            }
        }
        public void MixedStatesWithExistingParentAndVaryingChildrenisUnderstood()
        {
            var ninja  = EF6Model.RichModels.Ninja.CreateIndependent("julie", true);
            var pNinja = new PrivateObject(ninja);

            pNinja.SetProperty("Id", 1);
            var originalOwnerId = 99;
            var equip           = EF6Model.RichModels.NinjaEquipment.Create(originalOwnerId, "arrow");

            ninja.TransferEquipmentFromAnotherNinja(equip);
            using (var context = new NinjaContext()) {
                context.Ninjas.Attach(ninja);
                var entries = context.ChangeTracker.Entries();
                OutputState(entries); context.FixState();
                entries.ToList().ForEach(e => Debug.WriteLine($"After:{e.Entity.ToString()}:{e.State}"));
                Assert.IsTrue(entries.Any(e => e.State == EntityState.Modified));
            }
        }