我有一些迁移将一些数据插入到数据库中,这就是其中之一:
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
,因此有人知道解决方法吗?