private void DbPropertyEntry_method_can_be_used_on_previously_detached_entry_after_the_entity_becomes_tracked( Action<DbPropertyEntry> test) { using (var context = new F1Context()) { var team = new Team { Id = -1, Name = "Wubbsy Racing", Chassis = new Chassis { TeamId = -1, Name = "Wubbsy" } }; var entry = context.Entry(team).Property(t => t.Name); context.Teams.Attach(team); test(entry); // Just testing here that this doesn't throw } }
// Note that simple cases such as nulls that don't involve EF metadata are tested in the unit tests private DbEntityEntry<Team> GetTeamEntry(F1Context context) { var team = new Team { Id = -1, Name = "Wubbsy Racing", Chassis = new Chassis { TeamId = -1, Name = "Wubbsy" } }; team.Engine = new Engine { Id = -2, Name = "WubbsyV8", Teams = new List<Team> { team }, Gearboxes = new List<Gearbox>() }; context.Teams.Attach(team); return context.Entry(team); }
private void Current_reference_value_for_one_to_one_principal_can_be_read_and_set(EntityState state) { using (var context = new F1Context()) { var chassisEntry = context.Entry(GetTeamEntry(context).Entity.Chassis); chassisEntry.State = state; var refEntry = chassisEntry.Reference(c => c.Team); var value = refEntry.CurrentValue; Assert.Same(chassisEntry.Entity.Team, value); if (state == EntityState.Unchanged || state == EntityState.Modified) { // Changing the reference to the principal will cause EF to throw a referential integrity exception // because it would need a change in the PK of the dependent. Assert.Throws<InvalidOperationException>(() => refEntry.CurrentValue = new Team()).ValidateMessage( "EntityReference_CannotChangeReferentialConstraintProperty"); } else { value = new Team(); refEntry.CurrentValue = value; Assert.Same(chassisEntry.Entity.Team, value); if (state == EntityState.Deleted) { Assert.Throws<InvalidOperationException>(() => context.ChangeTracker.DetectChanges()). ValidateMessage("RelatedEnd_UnableToAddRelationshipWithDeletedEntity"); } else { context.ChangeTracker.DetectChanges(); } value = refEntry.CurrentValue; Assert.Same(chassisEntry.Entity.Team, value); } refEntry.CurrentValue = null; Assert.Null(refEntry.CurrentValue); Assert.Null(chassisEntry.Entity.Team); context.ChangeTracker.DetectChanges(); } }
public void Adding_entity_of_wrong_type_to_non_generic_local_view_throws() { var team = new Team { Id = -1, Name = "Wubbsy Racing" }; var expectedException = GenerateException(() => ((IList)new List<Driver>()).Add(team)); using (var context = new F1Context()) { var local = context.Set(typeof(Driver)).Local; Assert.Equal(expectedException.Message, Assert.Throws<ArgumentException>(() => local.Add(team)).Message); } }