0

I've followed the examples like accepted here: Possible to default DateTime field to GETDATE() with Entity Framework Migrations?

In Sql server, the "Default Binding or Value" is now getting set to ('1900-01-01T00:00:00.000'), but not to GetDate() as I would expect.

public partial class InitialCreate : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.UserWordsModels", "DateAdded", c => c.DateTime(defaultValueSql: "GETDATE()"));

    }

    public override void Down()
    {
        DropColumn("dbo.UserWordsModels", "DateAdded");
    }
}

In the Model:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime DateAdded { get; set; }

One thing I may have different is I set this so all DateTime fields are DateTime2

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Properties<DateTime>().Configure(c => c.HasColumnType("datetime2"));}
VirtualLife
  • 402
  • 5
  • 14
  • What's the problem you are experiencing with `defaultValueSql: "GETDATE()"`? – Ivan Stoev Jun 02 '19 at 11:28
  • It's not setting GetDate() in sql, it's setting it as ('1900-01-01T00:00:00.000'). Sorry if that wasn't clear. – VirtualLife Jun 02 '19 at 11:39
  • The database-default value is only set if no value is provided in the INSERT/UPDATE statement. You apparently send DateTIme.MinValue. – Gert Arnold Jun 02 '19 at 13:16
  • That isn't the value inserted, that is the value in the "Default Binding or Value" which should be GetDate(), not ('1900-01-01T00:00:00.000'). – VirtualLife Jun 02 '19 at 13:20

0 Answers0