0

我有一些迁移将一些数据插入到数据库中,这就是其中之一:

 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,因此有人知道解决方法吗?

4

2 回答 2

1

您可以通过将其包装在sqlDataTime结构中来解决您的问题,例如:

new SqlDateTime (DateTime value) 

如果它是 poco 对象:
您可以尝试使用这样的数据注释指定列类型

[Column(TypeName = "datetime")]
DateTime CreatedOn {get;set;}

查看文档

于 2020-01-07T09:00:37.133 回答
-1

好吧,问题不在于毫秒。我AddTicks从迁移中删除了几毫秒:

  migrationBuilder.InsertData(
            table: "TSegment",
            columns: new[] { "SegmentId", "CreatedOn", "ParentId", "SegmentType", "UpdatedOn" },
            values: new object[,]
            {
                { 1, new DateTime(2019, 1, 22, 12, 24, 23, DateTimeKind.Utc), null, "YouAndYourFamily", new DateTime(2019, 1, 22, 12, 24, 23,DateTimeKind.Utc) },
                { 2, new DateTime(2019, 1, 22, 12, 24, 23, DateTimeKind.Utc), 1, "AboutYou", new DateTime(2019, 1, 22, 12, 24, 23,DateTimeKind.Utc)},
                { 3, new DateTime(2019, 1, 22, 12, 24, 23, DateTimeKind.Utc), 1, "YourFamily", new DateTime(2019, 1, 22, 12, 24, 23,DateTimeKind.Utc) },
                { 4, new DateTime(2019, 1, 22, 12, 24, 23, DateTimeKind.Utc), 1, "Employment", new DateTime(2019, 1, 22, 12, 24, 23,DateTimeKind.Utc) },
                { 5, new DateTime(2019, 1, 22, 12, 24, 23, DateTimeKind.Utc), null, "HomeAndContact", new DateTime(2019, 1, 22, 12, 24, 23,DateTimeKind.Utc) },
                { 6, new DateTime(2019, 1, 22, 12, 24, 23, DateTimeKind.Utc), 5, "YourHome", new DateTime(2019, 1, 22, 12, 24, 23,DateTimeKind.Utc) },
                { 7, new DateTime(2019, 1, 22, 12, 24, 23, DateTimeKind.Utc), 5, "ContactDetails", new DateTime(2019, 1, 22, 12, 24, 23,DateTimeKind.Utc) }
            });

现在生成以下脚本:

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.0000000Z', NULL, N'YouAndYourFamily', '2019-01-22T12:24:23.0000000Z'),
(2, '2019-01-22T12:24:23.0000000Z', 1, N'AboutYou', '2019-01-22T12:24:23.0000000Z'),
(3, '2019-01-22T12:24:23.0000000Z', 1, N'YourFamily', '2019-01-22T12:24:23.0000000Z'),
(4, '2019-01-22T12:24:23.0000000Z', 1, N'Employment', '2019-01-22T12:24:23.0000000Z'),
(5, '2019-01-22T12:24:23.0000000Z', NULL, N'HomeAndContact', '2019-01-22T12:24:23.0000000Z'),
(6, '2019-01-22T12:24:23.0000000Z', 5, N'YourHome', '2019-01-22T12:24:23.0000000Z'),
(7, '2019-01-22T12:24:23.0000000Z', 5, N'ContactDetails', '2019-01-22T12:24:23.0000000Z');
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 个小数位,并且会抛出这个异常:

Microsoft.Data.SqlClient.SqlException (0x80131904):从字符串转换日期和/或时间时转换失败。在 Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔 breakConnection,Action`1 wrapCloseInAction)在 Microsoft.Data.SqlClient .TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 在 Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean & dataReady) 在 Microsoft.Data.SqlClient。 SqlCommand.RunExecuteNonQueryTds(字符串方法名,

我错过了什么吗?

于 2020-01-07T14:42:34.123 回答