-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Support the new BCL DateOnly and TimeOnly structs for SQL Server #24507
Comments
Please note that the final names for these types are |
Clearing milestone to discuss doing this in 6.0.0. |
|
As of NET 6 Preview 6 I get an error using EF Core mapping DateOnly properties stating that the DB Provider (SQL Server) does not support this type. Is it supported or not? If not, can HaveConversion be a workaround? so far nothing worked (except not using DateOnly). |
@CleanCodeX EF Core support for DateOnly/TimeOnly depends on lower-level support in SqlClient; that's tracked in dotnet/SqlClient#1009. |
As already discussed there, it's not supported. What's the common workaround? |
@CleanCodeX there isn't anything built-in for SQL Server, you'll have to implement your own value converter. |
So I did. Edit: missing call to HaveColumnType added public class YourDbContext : DbContext
{
Ctors() {...}
protected override void ConfigureConventions(ModelConfigurationBuilder builder)
{
builder.Properties<DateOnly>()
.HaveConversion<DateOnlyConverter>()
.HaveColumnType("date");
builder.Properties<DateOnly?>()
.HaveConversion<NullableDateOnlyConverter>()
.HaveColumnType("date");
}
}
/// <summary>
/// Converts <see cref="DateOnly" /> to <see cref="DateTime"/> and vice versa.
/// </summary>
public class DateOnlyConverter : ValueConverter<DateOnly, DateTime>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public DateOnlyConverter() : base(
d => d.ToDateTime(TimeOnly.MinValue),
d => DateOnly.FromDateTime(d))
{ }
}
/// <summary>
/// Converts <see cref="DateOnly?" /> to <see cref="DateTime?"/> and vice versa.
/// </summary>
public class NullableDateOnlyConverter : ValueConverter<DateOnly?, DateTime?>
{
/// <summary>
/// Creates a new instance of this converter.
/// </summary>
public NullableDateOnlyConverter() : base(
d => d == null
? null
: new DateTime?(d.Value.ToDateTime(TimeOnly.MinValue)),
d => d == null
? null
: new DateOnly?(DateOnly.FromDateTime(d.Value)))
{ }
} I hope the dev community must not wait until NET 7, that a basic converter like this is added internally so users can use the short version of it: protected override void ConfigureConventions(ModelConfigurationBuilder builder)
{
builder.Properties<DateOnly>().HaveConversion<DateTime>();
// OR
builder.Properties<DateOnly>().HaveConversion<DateOnlyToDateTimeConverter>();
} Thanks for your time. Keep up the good work. |
It isn't just "a basic converter" because you are still using datetime in the database. It is a great workaround for anyone who want to just map to Just being fair with the team working on this ;) |
That's true, though it's possible to map DateTime to SQL Server Another thing missing from the above is translations from properties/methods on DateOnly/TimeOnly; so this would only take care of reading and writing the values. |
Yeah of course the the properties needed to be marked as date / time sql type (or with fluent api). I thought that would be clear then. My mistake if it wasn't. Edit: protected override void ConfigureConventions(ModelConfigurationBuilder builder)
{
builder.Properties<DateOnly>()
.HaveConversion<DateOnlyConverter, DateOnlyComparer>()
.HaveColumnType("date");
} |
What is the status on this with RC1? Do we still need to write custom converters? |
DateOnly and TimeOnly support for SQL Server is not ready yet, earliest EF Core 7 is my guess. |
This comment has been minimized.
This comment has been minimized.
Have these been removed in .net 7 and 2019 I get []( at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0() |
@davidbuckleyni DateOnly and TimeOnly aren't yet supported when using the SQL Server provider, that's what this issue tracks. |
I have just published a preview package that adds support for DateOnly and TimeOnly with EF Core 6 and 7. Feel free to give it a try and provide feedback in the github repo. |
@ErikEJ your timing couldn't have been more perfect for me. Giving it a whirl now. |
doesnt the existence of ErikEJ.EntityFrameworkCore.SqlServer.DateOnlyTimeOnly imply that it should be possible for DateOnly and TimeOnly support to be added into the main EF codebase without waiting for a SqlClient update? @roji |
@SimonCropp no. My extension depends on SqlClient 5.1 preview. If a LTS version is released before EF Core 8 it will be possible. |
@SimonCropp yeah, as @ErikEJ wrote, an LTS version of SqlClient with DateOnly/TimeOnly support is expected to land before EF Core 8.0 is released (in fact, it may quite soon). Once that happens, we can add native support in the EF SQL Server provider. |
We ran into this and we've had to use a work around. Many thanks Erik for your work unblocking this. |
Is there any way to add this support to scaffold as well? Just to not override types manually after generation with db-first |
@Lonli-Lokli My package should support Scaffolding as well. But I will remind myself to add to EF Core Power Tools actually! ErikEJ/EFCorePowerTools#1642 |
I new this ages ago dont no why only replying now |
New DateOnly and TimeOnly structs are being introduced to .NET 6 as alternatives to DateTime (dotnet/runtime#49036). This issue tracks supporting them in SQL Server.
I've opened a separate SqlClient issue for support (dotnet/SqlClient#1009).
The text was updated successfully, but these errors were encountered: