首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EF核心DateTimeOffset

EF核心DateTimeOffset
EN

Stack Overflow用户
提问于 2022-05-07 22:55:26
回答 1查看 841关注 0票数 0

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

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

属性“MyProp”的类型为“DateTimeOffset”,当前数据库提供程序不支持该类型。要么更改属性CLR类型,要么使用'NotMapped‘属性忽略该属性,或者在'OnModelCreating’中使用'EntityTypeBuilder.Ignore‘。

为什么不能将C#类型DateTimeOffset映射到Server类型datetimeoffset (文档)?

编辑:添加Stacktrace

代码语言:javascript
复制
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上有一个复制。

EN

回答 1

Stack Overflow用户

发布于 2022-05-08 10:01:45

当仔细观察数据模型时,这个问题是显而易见的。我已经更改了属性的类型,但是EF类型名称是使用属性[Column("CreateDate", TypeName = "datetime")]固定的。

代码语言:javascript
复制
[Column("CreateDate", TypeName = "datetime")]
[Required]
public DateTimeOffset CreateDate { get; set; }

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

代码语言:javascript
复制
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. 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72156907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档