Fluent1 API – 在日期字段传递空值时如何添加默认日期(1900-01-01)

huangapple go评论56阅读模式
英文:

Fluent1 API - How to add default date while passing null value in date filed (1900-01-01)

问题

通过Fluent API在.NET Core中为日期字段添加默认值的方法如下:

builder.Property(e => e.DOB)
       .HasColumnType("date")
       .HasDefaultValueSql("getdate()");

但是这似乎没有生效。

英文:

How to add default value for date field through fluent API entity framework in .net core.
my code is below :-

builder.Property(e => e.DOB)
.HasColumnType("date")
.HasDefaultValueSql("getdate()");

but this not working.

答案1

得分: 0

> 如何通过fluent API实体框架为日期字段添加默认值在.NET Core中

实际上,可以通过多种方式生成默认日期值。换句话说,Entity Framework Core提供程序通常不会自动为日期/时间列设置值生成 - 您必须自行配置。

此外,根据您的情况,您可以按照以下步骤操作:

builder.Property(e => e.DOB).HasDefaultValueSql("getdate()");

注意: 欲了解更多详细信息,您可以查看官方文档

英文:

> How to add default value for date field through fluent API entity
> framework in .net core

Actually, defualt date value can be generated in various ways. In other words, entity framework core providers usually don't set up value generation automatically for date/time columns - you have to configure this yourself.

In addition, based on your scenario, you could do as following:

builder.Property(e => e.DOB).HasDefaultValueSql("getdate()");

Note: For more details, you could have a look at this official document.

huangapple
  • 本文由 发表于 2023年5月18日 12:28:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定