当前位置:网站首页>How to make ef core 6 support dateonly type
How to make ef core 6 support dateonly type
2022-06-27 03:47:00 【Dotnet cross platform】
Preface
The last time , We found that 《DateOnly and TimeOnly Types cannot be serialized 》.
But the problem is more than that .
Problem recurrence
Suppose you have the following entity classes :
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public DateOnly Birthday { get; set; }
} because Birthday Just the date , So we used DateOnly type .
however , In the use of EF Core 6 When stored in the database , The following error occurred :
Because the database does not DateOnly type , We use datetime type
result DateOnly Can not be Map become datetime Database type .
heavy load OnModelCreating
According to the prompt , We've overloaded OnModelCreating Method , Find a HasColumnType Method , Used to configure the data type of the column to which the attribute is mapped when facing the relational database :
modelBuilder.Entity<User>()
.Property(t => t.Birthday)
.HasColumnType("datetime");however , No effect .
HasConversion Method
Continue to find , There's another one HasConversion Method , Used to configure properties , To convert attribute values before writing to the database and back when reading from the database :
modelBuilder.Entity<User>()
.Property(t => t.Birthday)
.HasConversion(
d => d.ToDateTime(TimeOnly.MinValue),
d => DateOnly.FromDateTime(d)); Now? ,EF Core 6 Have been able to handle correctly DateOnly, And use datetime Database type to store it .
Conclusion
today , We introduced the use of converters to tell EF Core 6 How to deal with it DateOnly.
Add microsignals 【MyIO666】, Invite you to join the technical exchange group
边栏推荐
- 2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答
- 为什么 C# 访问 null 字段会抛异常?
- Il manque beaucoup de fichiers et de répertoires tels que scripts pendant et après l'installation d'anaconda3
- 超级详细,2 万字详解,吃透 ES!
- PAT甲级 1018 Public Bike Management
- 敏捷开发篇--Agile Development-自用
- Record the method of reading excel provided by unity and the solution to some pits encountered
- 2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
- PAT甲级 1023 Have Fun with Numbers
- mysql数据库基础:DQL数据查询语言
猜你喜欢

2021:Greedy Gradient Ensemble for Robust Visual Question Answering

Agile development - self use

2021:Greedy Gradient Ensemble for Robust Visual Question Answering

2016Analyzing the Behavior of Visual Question Answering Models

PAT甲级 1026 Table Tennis

2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering

Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?

Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal

Record the method of reading excel provided by unity and the solution to some pits encountered

GAMES101作业7提高-微表面材质的实现过程
随机推荐
[数组]BM94 接雨水问题-较难
2022 Chinese pastry (Advanced) recurrent training question bank and online simulation test
How do I simplify the development of interfaces in open source systems?
Ldr6028 OTG data transmission scheme for mobile devices while charging
Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal
promise源码-class版本【三、Promise源码】【代码详细注释/测试案例完整】
事业观、金钱观与幸福观
PAT甲级 1019 General Palindromic Number
[promise I] introduction of promise and key issues of hand rolling
对数器
2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验
2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答
文旅灯光秀打破时空限制,展现景区夜游魅力
如何让 EF Core 6 支持 DateOnly 类型
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
2021:Check it again:Progressive Visual Question Answering via Visual Entailment通过视觉暗示进行渐进式视觉问答
PAT甲级 1020 Tree Traversals
Argo Workflows —— Kubernetes的工作流引擎入门
Pat grade a 1019 general palindromic number
TP5 spreadsheet excel table export