3

Is there a way to make Entity Framework Core let you use a DateTime for a SQL Time column? SQL Server allows you to set a Time column value using a DateTime, but when you try to configure Entity Framework Core to do this, it throws an exception when compiling the model saying that the Database Provider does not support the datatype.

This code throws the exception

public void Configure(EntityTypeBuilder<Category> builder)
{
    ...
    builder.Property<System.DateTime>(x => x.StartTime).HasColumnName(@"StartTime").HasColumnType(@"time").IsRequired() 
    // HasColumnType matches the type defined in SQL
}

This code works fine, but I'd rather keep HasColumnType accurate so that it doesn't modify the database to use DateTime instead of Time...

public void Configure(EntityTypeBuilder<Category> builder)
{
    ...
    builder.Property<System.DateTime>(x => x.StartTime).HasColumnName(@"StartTime").HasColumnType(@"datetime").IsRequired()
    // HasColumnType does not match the type defined in SQL
}

Is there a reason EF Core blocks the ability to do this when SQL allows it?

10
  • Use Timespan instead. A time column contains only the time-of-day, it's not a full datetimeso it shouldn't be mapped to DateTime Commented Jun 30, 2020 at 16:46
  • I prefer to avoid TimeSpan, it's just a terrible DataType for dealing with the Time of Day, you need a custom binder to map datetime values coming from the client... And SQL Server supports setting a Time field with a DateTime, so why would Entity Framework arbitrarily block that functionality?. Commented Jun 30, 2020 at 17:39
  • Because SQL Server doesn't support that at all. The legacy datetime type has a lot of quirks - you could add two datetime values and get a date in 4040 for example. That doesn't mean it's a good idea. That's why SQL Server doesn't allow implicit conversions between time and the newer types like datetime2 Commented Jun 30, 2020 at 17:51
  • As for terrible types, it's DateTime that's terrible. Time-of-Day doesn't have a date component at all, so what is a DateTime supposed to mean? How could you compare times when all you have are DateTime values? While Timespan may not be restricted to just 24 hours, it's a lot better than DateTime though. There's no need for a custom binder either - time maps to Timespan, not DateTime Commented Jun 30, 2020 at 17:52
  • In the end, time map to Timespan in EF. Commented Jun 30, 2020 at 17:53

1 Answer 1

3

I ended up resolving this with a Conversion

builder.Property<DateTime>("StartTime").HasColumnName(@"StartTime").HasColumnType(@"time").IsRequired().HasConversion(v => v.TimeOfDay, v => DateTime.Now.Date.Add(v));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.