After the query statement ( db.Vehicles.Single ... ), the property is null because you are not loading it. Assigning a different value to it does not cause a lazy load, so nothing changes here.
Only when the property is loaded, the assignment (any assignment that also replaces it with another object) will have an effect. If the property is not loaded, the change tracker does not track anything.
The property can be loaded by including it in the request
db.Vehicles.Include(v => v.ParkingBay)...
or by subsequently accessing it in code, for example
var pb = vehicle.ParkingBay;
or by checking it in the debugger (watch or quickview), which also launches lazy loading.
Include is the recommended approach if you intend to apply any changes to the navigation properties yourself.
As noted below, a more efficient way to clear the navigation reference property is to set the model to a primitive foreign key value and set it to null . In your case, will it be something like int? ParkingBayId int? ParkingBayId . This pattern is known as foreign key associations, unlike independent associations when only a reference property is present.
Gert arnold
source share