EF Core DateTimeOffset



我正在尝试将使用DateTime属性的模型迁移到DateTimeOffset属性。

创建迁移时,我得到以下错误:

属性'MyProp'的类型是'DateTimeOffset',目前不支持数据库供应商。更改属性CLR类型,或者使用'[NotMapped]'属性或使用'EntityTypeBuilder。忽略'OnModelCreating'中的'。

为什么c#类型DateTimeOffset不能映射到SQL Server类型datetimeoffset(Docs)?

编辑:添加Stacktrace
System.InvalidOperationException: The property 'MyProp' is of type 'DateTimeOffset' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidatePropertyMapping(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)

编辑添加复制

GitHub上有一个复制。

仔细观察数据模型时,问题很明显。我已将属性的类型更改为DateTimeOffset,但EF Core类型名称使用属性[Column("CreateDate", TypeName = "datetime")]固定。这会产生歧义。

[Column("CreateDate", TypeName = "datetime")]
[Required]
public DateTimeOffset CreateDate { get; set; }

阅读异常消息让我有点困惑,因为它声明不支持类型DateTimeOffset

The property 'CreateDate' could not be mapped because it is of type 'DateTimeOffset', which is not a supported primitive type or a valid entity type. 

最新更新