public void Should_Create_Booking() { BookingContext db = new BookingContext(); var city = new City() { Id = 1 }; var booking = new Booking(); booking.Status = BookingStatus.New; booking.CityId = 1; //_db.Cities.Attach(city); db.Bookings.AddObject(booking); db.SaveChanges(); }
private void FixupCity(City previousValue) { if (IsDeserializing) { return; } if (City != null) { CityId = City.Id; } if (ChangeTracker.ChangeTrackingEnabled) { if (ChangeTracker.OriginalValues.ContainsKey("City") && (ChangeTracker.OriginalValues["City"] == City)) { ChangeTracker.OriginalValues.Remove("City"); } else { ChangeTracker.RecordOriginalValue("City", previousValue); } if (City != null && !City.ChangeTracker.ChangeTrackingEnabled) { City.StartTracking(); } } }
private void FixupCity(City previousValue) { // This is the dependent end in an association that performs cascade deletes. // Update the principal's event listener to refer to the new dependent. // This is a unidirectional relationship from the dependent to the principal, so the dependent end is // responsible for managing the cascade delete event handler. In all other cases the principal end will manage it. if (previousValue != null) { previousValue.ChangeTracker.ObjectStateChanging -= HandleCascadeDelete; } if (City != null) { City.ChangeTracker.ObjectStateChanging += HandleCascadeDelete; } if (IsDeserializing) { return; } if (City != null) { CityId = City.Id; } if (ChangeTracker.ChangeTrackingEnabled) { if (ChangeTracker.OriginalValues.ContainsKey("City") && (ChangeTracker.OriginalValues["City"] == City)) { ChangeTracker.OriginalValues.Remove("City"); } else { ChangeTracker.RecordOriginalValue("City", previousValue); // This is the dependent end of an identifying association, so it must be deleted when the relationship is // removed. If the current state is Added, the relationship can be changed without causing the dependent to be deleted. // This is a unidirectional relationship from the dependent to the principal, so the dependent end is // responsible for cascading the delete. In all other cases the principal end will manage it. if (previousValue != null && ChangeTracker.State != ObjectState.Added) { this.MarkAsDeleted(); } } if (City != null && !City.ChangeTracker.ChangeTrackingEnabled) { City.StartTracking(); } } }