我正在尝试使用DateTime属性将模型迁移到DateTimeOffset属性。
在创建迁移时,我会得到以下错误:
属性“MyProp”的类型为“DateTimeOffset”,当前数据库提供程序不支持该类型。要么更改属性CLR类型,要么使用'NotMapped‘属性忽略该属性,或者在'OnModelCreating’中使用'EntityTypeBuilder.Ignore‘。
为什么不能将C#类型DateTimeOffset映射到Server类型datetimeoffset (文档)?
编辑:添加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上有一个复制。
发布于 2022-05-08 10:01:45
当仔细观察数据模型时,这个问题是显而易见的。我已经更改了属性的类型,但是EF类型名称是使用属性[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. https://stackoverflow.com/questions/72156907
复制相似问题