【发布时间】:2020-01-07 08:55:40
【问题描述】:
我有一些迁移将一些数据插入到数据库中,这就是其中之一:
migrationBuilder.InsertData(
table: "TSegment",
columns: new[] { "SegmentId", "CreatedOn", "ParentId", "SegmentType", "UpdatedOn" },
values: new object[,]
{
{ 1, new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(4948), null, "YouAndYourFamily", new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(5782) },
{ 2, new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6649), 1, "AboutYou", new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6658) },
{ 3, new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6670), 1, "YourFamily", new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6676) },
{ 4, new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6679), 1, "Employment", new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6685) },
{ 5, new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6688), null, "HomeAndContact", new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6694) },
{ 6, new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6700), 5, "YourHome", new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6703) },
{ 7, new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6709), 5, "ContactDetails", new DateTime(2019, 1, 22, 12, 24, 23, 48, DateTimeKind.Utc).AddTicks(6712) }
});
将 EF Core 升级到版本 3 后,上面的代码会生成此插入:
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'SegmentId', N'CreatedOn', N'ParentId', N'SegmentType', N'UpdatedOn') AND [object_id] = OBJECT_ID(N'[TSegment]'))
SET IDENTITY_INSERT [TSegment] ON;
INSERT INTO [TSegment] ([SegmentId], [CreatedOn], [ParentId], [SegmentType], [UpdatedOn])
VALUES (1, '2019-01-22T12:24:23.0484948Z', NULL, N'YouAndYourFamily', '2019-01-22T12:24:23.0485782Z'),
(2, '2019-01-22T12:24:23.0486649Z', 1, N'AboutYou', '2019-01-22T12:24:23.0486658Z'),
(3, '2019-01-22T12:24:23.0486670Z', 1, N'YourFamily', '2019-01-22T12:24:23.0486676Z'),
(4, '2019-01-22T12:24:23.0486679Z', 1, N'Employment', '2019-01-22T12:24:23.0486685Z'),
(5, '2019-01-22T12:24:23.0486688Z', NULL, N'HomeAndContact', '2019-01-22T12:24:23.0486694Z'),
(6, '2019-01-22T12:24:23.0486700Z', 5, N'YourHome', '2019-01-22T12:24:23.0486703Z'),
(7, '2019-01-22T12:24:23.0486709Z', 5, N'ContactDetails', '2019-01-22T12:24:23.0486712Z');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'SegmentId', N'CreatedOn', N'ParentId', N'SegmentType', N'UpdatedOn') AND [object_id] = OBJECT_ID(N'[TSegment]'))
SET IDENTITY_INSERT [TSegment] OFF;
我们可以看到,日期毫秒有 6 位小数,但如果我将 EF Core 降级到版本 2,相同的迁移会生成此插入:
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'SegmentId', N'CreatedOn', N'ParentId', N'SegmentType', N'UpdatedOn') AND [object_id] = OBJECT_ID(N'[TSegment]'))
SET IDENTITY_INSERT [TSegment] ON;
INSERT INTO [TSegment] ([SegmentId], [CreatedOn], [ParentId], [SegmentType], [UpdatedOn])
VALUES (1, '2019-01-22T12:24:23.048', NULL, N'YouAndYourFamily', '2019-01-22T12:24:23.048'),
(2, '2019-01-22T12:24:23.048', 1, N'AboutYou', '2019-01-22T12:24:23.048'),
(3, '2019-01-22T12:24:23.048', 1, N'YourFamily', '2019-01-22T12:24:23.048'),
(4, '2019-01-22T12:24:23.048', 1, N'Employment', '2019-01-22T12:24:23.048'),
(5, '2019-01-22T12:24:23.048', NULL, N'HomeAndContact', '2019-01-22T12:24:23.048'),
(6, '2019-01-22T12:24:23.048', 5, N'YourHome', '2019-01-22T12:24:23.048'),
(7, '2019-01-22T12:24:23.048', 5, N'ContactDetails', '2019-01-22T12:24:23.048');
IF EXISTS (SELECT * FROM [sys].[identity_columns] WHERE [name] IN (N'SegmentId', N'CreatedOn', N'ParentId', N'SegmentType', N'UpdatedOn') AND [object_id] = OBJECT_ID(N'[TSegment]'))
SET IDENTITY_INSERT [TSegment] OFF;
由于 SQL Server 的精度为小数点后 3 位,我无法将所有日期时间列更改为 Datetime2,有人知道解决方法吗?
【问题讨论】:
-
什么是表列类型?当列是
datetime2(0)或datetime2(3)时,存储微秒是没有意义的。 -
顺便说一句,你为什么要使用 ticks?你真的想节省微秒吗?在这种情况下,您必须使用适当的类型。
datetime没有所需的精度。您必须使用datetime2(7)并 在列注释中指定该精度。即使这样,您也可以获得 100ns 的准确度 -
这个问题看了两遍,一头雾水。如果你只想要 毫秒,那么使用刻度是没有意义的。事实上,EF Core 2 的截断应该被视为一个 bug -
datetimedoesn't even have millisecond precision。您可以获得.047或.050。所有这些值都将作为.050存储在数据库中。你有实际问题吗?或者您是否依赖错误的实现来获得.047而不是.050?
标签: sql-server ef-core-3.1 entity-framework-core-3.1