Pomelo.entityframeworkcore.mysql: How to correctly configure Default Value for CURRENT_TIMESTAMP

Created on 4 Aug 2016  路  5Comments  路  Source: PomeloFoundation/Pomelo.EntityFrameworkCore.MySql

Steps To Reproduce

From the docs sample: https://ef.readthedocs.io/en/latest/modeling/relational/default-values.html

``` c#
class MyContext : DbContext
{
public DbSet Blogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .Property(b => b.Created)
            .HasDefaultValueSql("CURRENT_TIMESTAMP");
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }
    public DateTime Created { get; set; }
}

```

The issue

The DB default in MySql is correct, but when saving this entity, without setting the Created property, it defaults to the DateTime Min of '0001-01-01 00:00:00'. If you do an insert directly in MySql it obviously works fine.
I would assume the correct behavior would be to read back the value or use in the sql statment 'UTC_TIMESTAMP()'.

I can workaround by setting date in code, but would be good to understand if this is just bug or not.

Further technical details

DotNet Core 1.0 RTM
MySQL version: 5.7
Pomelo.EntityFrameworkCore.MySql version: 1.0.0-prerelease-20160803

closed-fixed type-bug

Most helpful comment

Hi @greghroberts. DateTime is a value type in .net. The default value is going to be default(DateTime) if you did not specify a default value, is '0001-01-01 00:00:00'.

You can also define a default value in your model:

c# public DateTime Created { get; set; } = DateTime.Now;

All 5 comments

Hi @greghroberts. DateTime is a value type in .net. The default value is going to be default(DateTime) if you did not specify a default value, is '0001-01-01 00:00:00'.

You can also define a default value in your model:

c# public DateTime Created { get; set; } = DateTime.Now;

I understand it's a value type. My question is more on if you plan on making DefaultValues work as per the EF docs. The attached example and link do not work as designed.

@greghroberts Confirmed a bug, thanks for reporting this issue, we will fix it in some hours.

@greghroberts Thanks again. We have fixed this bug, and we will release this new version later.

Awesome thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hainguyenthanh picture hainguyenthanh  路  3Comments

leobert1226 picture leobert1226  路  3Comments

cetubig picture cetubig  路  3Comments

lebartha picture lebartha  路  3Comments

Toemsel picture Toemsel  路  3Comments