I have a POCO that can't have any nullable properties, for various reasons, but the database I'm connecting to has null values for some of these corresponding properties, so I have to handle these and set them to something else in the property getter/setter logic. But I'm still getting...
Constraint Exception was unhandled by user code: The 'DischargeDate' property on 'Visits' could not be set to a 'null' value. You must set this property to a non-null value of type 'System.DateTime'.
...Here's my property logic....
public class Visits
{
private DateTime _dischargeDate;
public DateTime DischargeDate
{
get {
if (this._dischargeDate == null)
{
this._dischargeDate = DateTime.MinValue;
return this._dischargeDate;
}
else
{
return this._dischargeDate;
}
}
set
{
if (value == null)
{
this._dischargeDate = DateTime.MinValue;
}
else
{
this._dischargeDate = value;
}
}
}
...and the DbContext is just straight forward, like...
public class MyDBContext : DbContext
{
public MyDBContext(string connection)
: base(connection)
{
}
public DbSet<Visits> Visits { get; set; }
}
I have no idea why I'm getting this error. It throws when the context is loaded. Or to be more specific, when I try to access the DbSet<Visit>
Visits, like _dbcontext.Visits;