当前位置:网站首页>如何让 EF Core 6 支持 DateOnly 类型
如何让 EF Core 6 支持 DateOnly 类型
2022-06-27 03:36:00 【dotNET跨平台】
前言
上次,我们发现《DateOnly 和 TimeOnly 类型居然不能序列化》。
但问题还不仅仅如此。
问题重现
假设有下列实体类:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public DateOnly Birthday { get; set; }
}由于Birthday只需要日期,所以我们使用了DateOnly类型。
但是,在使用 EF Core 6 存储到数据库时,出现如下错误:
由于数据库没有 DateOnly 类型,我们使用的是 datetime 类型
结果DateOnly不能被 Map 成datetime数据库类型。
重载 OnModelCreating
根据提示,我们重载了OnModelCreating方法,发现有个HasColumnType方法,用于配置属性在面向关系数据库时映射到的列的数据类型:
modelBuilder.Entity<User>()
.Property(t => t.Birthday)
.HasColumnType("datetime");但是,没有任何效果。
HasConversion 方法
继续查找,发现还有个HasConversion方法,用于配置属性,以便在写入数据库之前转换属性值并在从数据库中进行读取时转换回:
modelBuilder.Entity<User>()
.Property(t => t.Birthday)
.HasConversion(
d => d.ToDateTime(TimeOnly.MinValue),
d => DateOnly.FromDateTime(d));现在,EF Core 6 已经可以正确地处理 DateOnly,并使用datetime数据库类型来存储它。
结论
今天,我们介绍了使用转换器来告诉 EF Core 6 如何处理 DateOnly。
添加微信号【MyIO666】,邀你加入技术交流群
边栏推荐
- 通信中的机器学习最佳阅读资料列表
- Logarithm
- PAT甲级 1026 Table Tennis
- Pat grade a 1026 table tennis
- How can e-commerce products be promoted and advertised on Zhihu?
- Games101 job 7 improvement - implementation process of micro surface material
- Resnet152 pepper pest image recognition 1.0
- 2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答
- NestJS环境变量配置,解决如何在拦截器(interceptor)注入服务(service)的问题
- PAT甲级 1018 Public Bike Management
猜你喜欢

Pat grade a 1025 pat ranking

Career outlook, money outlook and happiness outlook

2021:Check it again:Progressive Visual Question Answering via Visual Entailment通过视觉暗示进行渐进式视觉问答

记录unity 自带读取excel的方法和遇到的一些坑的解决办法

一文教你Kali信息收集

Window 加密壳实现

2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers

JMeter distributed pressure measurement

GAMES101作业7提高-微表面材质的实现过程

2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
随机推荐
再探Handler(上)(Handler核心原理最全解析)
Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
2022 operation of simulated examination platform for tea artist (Senior) work license question bank
TP5 spreadsheet excel table export
人群模拟
Yiwen teaches you Kali information collection
Interview-01
A^2=e | the solution of the equation | what exactly can this equation tell us
Super détaillé, 20 000 caractères détaillés, mangez à travers es!
Promise [II. Promise source code] [detailed code comments / complete test cases]
2021:Greedy Gradient Ensemble for Robust Visual Question Answering
TechSmith Camtasia latest 2022 detailed function explanation Download
Paddlepaddle 21 is implemented based on dropout with 4 lines of code droplock
Method of decoding iPhone certificate file
Mmdetection valueerror: need at least one array to concatenate solution
Test the respective roles of nohup and &
与STM32或GD32替换说明
Flink learning 4:flink technology stack
PAT甲级 1019 General Palindromic Number
2021:Greedy Gradient Ensemble for Robust Visual Question Answering