I’m building a class called “User” which has a field of type “Category”.
public class User
{
public long Id { get; set; }
public virtual Category Category{ get; set; }
}
public class Category
{
public long Id { get; set;}
public string Name { get; set; }
}
Whenever this field gets a value, it is no longer possible to set a null value.
How can I set this field to null again?
I think this is something to do with lazy loading. Since you are not requested Category for the user it is still null, so setting it to null, will not trigger EF that there are changes on Category. You need to include Category in to the User, and than change that value. This way EF will see changes on Category and set it to null.
Try something like this: