I've got a table that has had a few new columns added, and they allow nulls to accurately accommodate the existing data. At the time those records were created the new columns did not exist, so a null accurately represents the state of the database at that time. For all new data I don't want to allow nulls. What is the best practice to manage that?
If I use .IsRequired()
in the EntityTypeConfiguration
class it will try to alter the table to make the column non-nullable and fail when it encounters the historic nulls. I still want to be able to read those old records, I just don't want to write any new records without the new column.
I want to do this as close to the DB as possible, so I've put some validation code in the setter.
private string _CountryCode;
public string CountryCode
{
get
{
return _CountryCode;
}
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("Country Code Required");
_CountryCode = value;
}
}
This does work, but I have a nagging feeling there is a cleaner solution out there.