Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to correctly configure Default Value for CURRENT_TIMESTAMP #35

Closed
greghroberts opened this issue Aug 4, 2016 · 5 comments
Closed
Assignees
Milestone

Comments

@greghroberts
Copy link

greghroberts commented Aug 4, 2016

Steps To Reproduce

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

class MyContext : DbContext
    {
        public DbSet<Blog> 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

@yukozh
Copy link
Member

yukozh commented Aug 4, 2016

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:

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

@yukozh yukozh closed this as completed Aug 4, 2016
@yukozh yukozh added this to the 1.0.0 milestone Aug 4, 2016
@yukozh yukozh self-assigned this Aug 4, 2016
@greghroberts
Copy link
Author

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.

@yukozh
Copy link
Member

yukozh commented Aug 4, 2016

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

@yukozh
Copy link
Member

yukozh commented Aug 4, 2016

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

@greghroberts
Copy link
Author

Awesome thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants