I have a SQL Server database. One of my tables has a SQL DATETIME
column. This column should not have to be entered when the record is created, but later should be modified.
I understand that DateTime
is not nullable. The default for datetime2
is 1/1/0001, while the minimum value for SQL Server is 1/1/1753.
Why when I insert a DateTime null I have "0001-01-01" in SQL Server?
So, my question is what is the correct way to do this? How do I set po.PODate
to the minimum for SQL Server (1/1/1753).
ApplicationDbContext db = new ApplicationDbContext();
PurchaseOrder po = new PurchaseOrder();
po.PODate = new DateTime(1753, 1, 1); // Best way?
// po.PODate = DateTime.MinValue; Doesn't work because DateTime.MinValue is 1/1/0001
// po.PODate = null; Doesn't work because DateTime is non-nullable value type
// po.PODate = DBNull.value; Doesn't work because it is for type System.DBNull not DateTime
db.PurchaseOrders.Add(po);
db.SaveChanges();