-
Notifications
You must be signed in to change notification settings - Fork 390
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
TimeSpan type does not map correctly to TIME MySQL type #1046
Comments
Take a look at Value Conversions, specifically |
@mguinness Thanks for the tip. Am i correct in interpreting the timespan converter as being translated to number of ticks, that is a Long in the database? |
This is expected. We currently handle As @mguinness pointed out, using entity.Property(e => e.BestServedBefore)
.HasColumnType("bigint")
.HasConversion(new TimeSpanToTicksConverter()); It is equivalent to the following code: entity.Property(e => e.BestServedBefore)
.HasColumnType("bigint")
.HasConversion(v => v.Ticks, v => new TimeSpan(v)); Here is a fully functional example: using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.Extensions.Logging;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Pomelo.EntityFrameworkCore.MySql.Storage;
namespace IssueConsoleTemplate
{
public class IceCream
{
public int IceCreamId { get; set; }
public string Name { get; set; }
public TimeSpan BestServedBefore { get; set; }
}
public class Context : DbContext
{
public DbSet<IceCream> IceCreams { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseMySql(
"server=127.0.0.1;port=3306;user=root;password=;database=Issue1046",
b => b
.ServerVersion(new ServerVersion("8.0.20-mysql"))
.CharSetBehavior(CharSetBehavior.NeverAppend))
.UseLoggerFactory(
LoggerFactory.Create(
b => b
.AddConsole()
.AddFilter(level => level >= LogLevel.Information)))
.EnableSensitiveDataLogging()
.EnableDetailedErrors();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<IceCream>(
entity =>
{
entity.Property(e => e.BestServedBefore)
.HasColumnType("bigint")
.HasConversion(new TimeSpanToTicksConverter());
entity.HasData(
new IceCream
{
IceCreamId = 1,
Name = "Vanilla",
BestServedBefore = new TimeSpan(0, 12, 0, 0)
},
new IceCream
{
IceCreamId = 2,
Name = "Chocolate",
BestServedBefore = new TimeSpan(0, 23, 59, 59)
},
new IceCream
{
IceCreamId = 3,
Name = "Artificial Vanilla",
// This will not work out-of-the box.
// Usage of a converter is necessary.
BestServedBefore = new TimeSpan(42, 11, 0, 0)
}
);
});
}
}
internal class Program
{
private static void Main()
{
using var context = new Context();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var iceCreams = context.IceCreams
.OrderBy(i => i.IceCreamId)
.ToList();
Debug.Assert(iceCreams.Count == 3);
Debug.Assert(iceCreams[0].BestServedBefore == new TimeSpan(0, 12, 0, 0));
Debug.Assert(iceCreams[1].BestServedBefore == new TimeSpan(0, 23, 59, 59));
Debug.Assert(iceCreams[2].BestServedBefore == new TimeSpan(42, 11, 0, 0));
}
}
} |
Create a model with a Timespan
The TimeSpan type creates a TIME typed column which has a smaller constraint than TimeSpan. Specifically TIME only goes from '-838:59:59.000000' to '838:59:59.000000' . while TimeSpan can reach 256204778:48:05.477580.
The text was updated successfully, but these errors were encountered: