コード例 #1
0
        private void Current_reference_value_for_one_to_many_principal_can_be_read_and_set(EntityState state)
        {
            using (var context = new F1Context())
            {
                var teamEntry = GetTeamEntry(context);
                teamEntry.State = state;
                var refEntry = teamEntry.Reference(t => t.Gearbox);

                var value = refEntry.CurrentValue;
                Assert.Same(teamEntry.Entity.Gearbox, value);

                value = new Gearbox { Id = -7 };
                refEntry.CurrentValue = value;
                Assert.Same(teamEntry.Entity.Gearbox, value);

                if (state != EntityState.Detached && state != EntityState.Deleted)
                {
                    // FK is fixed up without a call to DetectChanges
                    Assert.Equal(teamEntry.Entity.GearboxId, value.Id);
                }

                if (state == EntityState.Deleted)
                {
                    Assert.Throws<InvalidOperationException>(() => context.ChangeTracker.DetectChanges()).
                        ValidateMessage("RelatedEnd_UnableToAddRelationshipWithDeletedEntity");
                }
                else
                {
                    context.ChangeTracker.DetectChanges();
                }

                value = refEntry.CurrentValue;
                Assert.Same(teamEntry.Entity.Gearbox, value);

                refEntry.CurrentValue = null;
                Assert.Null(refEntry.CurrentValue);
                Assert.Null(teamEntry.Entity.Gearbox);
                if (state != EntityState.Detached)
                {
                    // FK is fixed up without a call to DetectChanges
                    // For Deleted state the FK should already have been null
                    Assert.Null(teamEntry.Entity.GearboxId);
                }
                context.ChangeTracker.DetectChanges();
            }
        }
コード例 #2
0
        private DbEntityEntry<Gearbox> GetGearboxEntry(F1Context context)
        {
            var team = GetTeamEntry(context).Entity;
            var gearbox = new Gearbox { Id = 1, Name = "WubbsyGears" };
            team.Gearbox = gearbox;
            team.Engine.Gearboxes.Add(gearbox);

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

            return context.Entry(gearbox);
        }