I have a Profile class that I'm trying to save to the database (MS SQL). In the database the fields are listed as 'NOT NULL'. The default/initial entry is going to be empty strings (string.empty).
I figured empty strings wouldn't fail for NULLs but it seems that EF is trying to pass them as NULL. Is that the case?
Here's some of the model class:
[Required]
public string Password { get; set; }
public string PasswordSalt { get; set; }
[Required]
[Display(Name = "Security Question")]
public string SecurityQuestion { get; set; }
[Required]
[Display(Name = "Security Answer")]
public string SecurityAnswer { get; set; }
That's the model. Here's the code that tries to set the data, using r as DataRow:
newProfile = new Profile
{
Salutation = r["Salutation"].ToString(),
FirstName = r["FirstName"].ToString(),
MiddleName = r["MiddleName"].ToString(),
LastName = r["LastName"].ToString(),
Biography = r["Biography"].ToString(),
Password = string.Empty,
PasswordSalt = string.Empty,
SecurityQuestion = string.Empty,
SecurityAnswer = string.Empty,
EnteredDate = DateTime.Now,
LastUpdatedDate = DateTime.Now,
RecordVersion = StaticTools.RecordVersion(),
};
_db.Profile.Add(newProfile);
try
{
_db.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
So, when we try SaveChanges() it fails with a DbValidationError, for Password, SecurityQuestion, SecurityAnswer.
"The Security Question field is required."
(Of course, we will not even check an empty password for login, the user will have to follow a registration email and set one.)