Pomelo.EntityFrameworkCore.MySql
Pomelo.EntityFrameworkCore.MySql copied to clipboard
The milliseconds of `DateTime` are lost when adding data seeds in database migration.
Question
The milliseconds of DateTime are lost when adding data seeds in database migration. After saving to the database, the milliseconds of the DateTime type in all entity-generated data seeds are lost. However, the milliseconds of data added using DbContext.Add are normal.
public class MyEntity
{
public Guid Id { get; set; }
public DateTime CreationTime { get; set; }
public DateTime UpdateTime { get; set; }
}
public class UtcDateTimeConverter : ValueConverter<DateTime, DateTime>
{
public UtcDateTimeConverter() : base(d => d.ToUniversalTime(),
d => DateTime.SpecifyKind(d, DateTimeKind.Utc))
{
}
}
Added migration code fregment in file Migration/20230719164925_Initial.cs :
migrationBuilder.InsertData(
table: "MyEntity",
columns: new[] { "Id", "CreationTime", "UpdateTime" },
values: new object[,]
{
{ new Guid("05460fb9-dd73-4936-4c61-08db8b12aec2"), new DateTime(2023, 7, 22, 16, 40, 51, 613, DateTimeKind.Utc).AddTicks(9050), new DateTime(2023, 7, 22, 16, 40, 51, 613, DateTimeKind.Utc).AddTicks(9050) },
{ new Guid("d518683e-24f7-40c2-4c60-08db8b12aec2"), new DateTime(2023, 7, 22, 16, 40, 51, 613, DateTimeKind.Utc).AddTicks(9040), new DateTime(2023, 7, 22, 16, 40, 51, 613, DateTimeKind.Utc).AddTicks(9050) }
});
As we can see, the DateTime contains the milliseconds.
Generated SQL script:
CREATE TABLE `MyEntity` (
`Id` char(36) COLLATE ascii_general_ci NOT NULL,
`CreationTime` datetime(6) NOT NULL,
`UpdateTime` datetime(6) NOT NULL,
CONSTRAINT `PK_MyEntity` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4;
INSERT INTO `MyEntity` (`Id`, `CreationTime`, `UpdateTime`)
VALUES ('cb0c4271-4ed1-4c5a-3077-08db7c5dfd15', TIMESTAMP '2023-07-04 07:12:13', TIMESTAMP '2023-07-04 07:12:13'),
('fe96bb85-7c49-4605-3076-08db7c5dfd15', TIMESTAMP '2023-07-04 07:12:13', TIMESTAMP '2023-07-04 07:12:13');
DateTime type is converted to TIMESTAMP, this may cause the issue. In table MyEntity, the type of column CreationTime is datetime(6)
Provider and version information
MySql: 5.7 EF Core version: 7.0.5 Database provider: Pomelo.EntityFrameworkCore.MySql 7.0.0 Target framework: .Net 7.0 Operating system: macOs Big Sur 11.7.7 IDE: JetBrains Rider 2023.1.3
I saw some cases failed about this, will try to fix in 8.0