Correct way to use default DateTime in ASP.NET MVC 5 -


i have sql server database. 1 of tables has sql datetime column. column should not have entered when record created, later should modified.

i understand datetime not nullable. default datetime2 1/1/0001, while minimum value sql server 1/1/1753.

why when insert datetime null have "0001-01-01" in sql server?

so, question correct way this? how set po.podate minimum 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 1/1/0001 // po.podate = null;               doesn't work because datetime non-nullable value type // po.podate = dbnull.value;       doesn't work because type system.dbnull not datetime  db.purchaseorders.add(po); db.savechanges(); 

if expect date not nullable, yes, have best way. ef pass in value field regardless, if don't, try pass in 1/1/0001 , error.

rather have define each time, create static class with:

public static datetime mindate() {    return new datetime(1753, 1, 1); } 

if wanted standardize it.

the other option make column nullable, , anytime query date out, have sql return 1/1/1753 when null.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -