I saw the similar questions but none of them satisfied my situation.
I have 2 tables:
User(id, apartmentId, ....)
Apartment(id, ....)
When User is created, the apartmentId is null, when user joined an apartment, the apartmentId will be set.
The problem is, after user LEFT his or her apartment, I need to set apartmentId to null, but because of the foreign key constraint MySql doesn't allow me to do so, but as you can see this is a make-sense case.
Other than dropping foreign key constraint, is there any other approach?
Here is my code
public bool QuitApartment(string userId)
{
var user = _userService.GetById(userId);
if(user == null)
{
return false;
}
user.ApartmentId = null;
_dataContext.Entry(user).State = EntityState.Modified;
_dataContext.SaveChanges();
return true;
}