从EF Core 2迁移到EF Core 3-日期时间毫秒精度

时间:2020-01-07 08:55:40

标签: sql-server ef-core-3.1 entity-framework-core-3.1

我进行了一些迁移,这些迁移将一些数据插入数据库,这就是其中之一:

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

1 个答案:

答案 0 :(得分:-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.SqlConnection.OnError(SqlException异常, 布尔值breakConnection,动作为1 wrapCloseInAction),位于 Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔值breakConnection,在以下位置执行Action'1 wrapCloseInAction) Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔值调用者HasConnectionLock,布尔值asyncClose) Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler,SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean&dataReady) Microsoft.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(字符串 methodName,布尔值isAsync,Int32超时,布尔值asyncWrite)位于 Microsoft.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 完成,布尔值sendToPipe,Int32超时,布尔值和usedCache, 布尔值asyncWrite,布尔值inRetry,字符串methodName)位于 Microsoft.Data.SqlClient.SqlCommand.ExecuteNonQuery()在 Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject) Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection 连接,IReadOnlyDictionary`2 parameterValues)位于 Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands,IRelationalConnection连接)位于 Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(字符串 targetMigration) Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade) IntelliFlo.Platform.EntityFramework.Database.Tasks.DatabaseMigrationTask.Run() 在 IntelliFlo.Platform.EntityFramework.Database.DatabaseInitializer.ExecuteTask(IDatabaseTask 任务)

我想念什么吗?