Search code examples
entity-frameworkasp.net-coreasp.net-web-apifluent

Fluent1 API - How to add default date while passing null value in date filed (1900-01-01)


How to add default value for date field through fluent API entity framework in .net core. my code is below :-

builder.Property(e => e.DOB)
.HasColumnType("date")
.HasDefaultValueSql("getdate()");

but this not working.


Solution

  • How to add default value for date field through fluent API entity framework in .net core

    Actually, defualt date value can be generated in various ways. In other words, entity framework core providers usually don't set up value generation automatically for date/time columns - you have to configure this yourself.

    In addition, based on your scenario, you could do as following:

    builder.Property(e => e.DOB).HasDefaultValueSql("getdate()");
    

    Note: For more details, you could have a look at this official document.