So I'm wondering about the difference between setting IsRequired() to false and simply not using it when setting up a property for a database table. Is the default behaviour when not using IsRequired() to allow the property to be nullable?
I don't have trial and error examples, just noticed on a project that whether it's false or not null, the table in the database displays the property as nullable either way. So is there a difference between not using IsRequired() at all or using it and setting it to false?
It's safe to assume that if you don't explictly declare a property as not-required, or you do so explicitly but set it to false, then you will get the same result.
I've had a quick dig around the EF Core source code, and the nearest I can come up with a definitive answer is the constructor for ForeignKey
:
public ForeignKey(
IReadOnlyList<Property> dependentProperties,
Key principalKey,
EntityType dependentEntityType,
EntityType principalEntityType,
ConfigurationSource configurationSource)
{
...
_isRequired = DefaultIsRequired;
and
private bool DefaultIsRequired
=> !Properties.Any(p => p.IsNullable);