【问题标题】:Returning Date instead of Datetime using modelBuilder conversion使用模型构建器转换返回日期而不是日期时间
【发布时间】:2020-07-21 05:59:34
【问题描述】:

我的模型中有一个 Datetime 属性,如下所示:

[DataType(DataType.Date)]
[Column(TypeName = "date")] 
public DateTime SubmittedOn { get; set; }

该值作为日期插入到数据库中,没有时间部分。但是,服务器会在请求该值时将其与时间部分一起返回,如下所示"2020-04-03T00:00:00"

如何在我的dbContext 中使用modelBuilder 来删除返回值时的时间部分?

modelBuilder.Entity<Model>()
              .Property(e => e.SubmittedOn)
              .HasConversion(...?)

【问题讨论】:

  • .NET没有日期类型,只有DateTime,所以总会有时间。
  • @JohnathanBarclay,当然,但进行转换并不意味着更改其数据类型。
  • 你可以在c#中做这样的事情docs.microsoft.com/en-us/dotnet/api/…我只是想把它集成到modelBuilder中
  • DateTime.Date 返回一个新的DateTime,时间部分设置为00:00;无论如何,这就是数据库返回的内容。你的时间有什么问题?使用[DataType(DataType.Date)] 表示时间无论如何都不会显示在 UI 中。
  • @JohnathanBarclay 您可以执行类似 date.ToString("MM/dd/yyyy") 的操作,并且您的日期没有时间部分格式化为字符串,您可以将其发送回客户端。我希望有办法做到这一点?我很想告诉你我的时间部分的问题,但这超出了这个问题的范围,所以如果这样做是不可能的,那么我只是有一个不同的问题:(

标签: c# .net-core entity-framework-core


【解决方案1】:

我相信只有日期部分保存在您的数据库中,因此返回的 DateTime 字段中的时间为 00:00:00。问题在于 JSON 序列化。据我记得 [DataType(DataType.Date)] 属性曾经为此工作,但现在由于某种原因不再起作用(欢迎任何关于此的 cmets)。 现在您可以在这里做两件事(请注意,这假设您正在使用 NewtonsoftJson 或愿意在 .net core 3.x 中使用它。对于 System.Text.Json,它是 .net core 3.x 中的默认序列化程序,您可以参考到这个Formatting DateTime in ASP.NET Core 3.0 using System.Text.Json):

  1. 制作一个自定义转换器,如下图:

    public class MyCustomDateConverter : IsoDateTimeConverter
    {
        public MyCustomDateConverter()
        {
            //Here you can define any format that is allowed in C#.
            DateTimeFormat = "dd/MM/yyyy";
        }
    }
    

    现在回到您将实际使用它的 DateTime 字段

    [JsonConverter(typeof(MyCustomDateConverter))]
    [Column(TypeName = "date")] 
    public DateTime SubmittedOn { get; set; }
    

    因此,您可以将转换器用于您想要的每个字段。

  2. 或者,如果您使用的是 asp.net 核心,则可以在以 JSON 序列化时为所有日期字段设置全局格式。在您的 Startup 类中将其添加到您的 ConfigureServices 方法中

    对于 pre .net core 3.x

    services.AddJsonOptions(
                options =>
                {
                    options.SerializerSettings.DateFormatString = "dd/MM/yyyy";
                }
            );
    

    对于 .net core 3.x

    services.AddControllers()
            .AddNewtonsoftJson(
                options =>
                {
                    options.SerializerSettings.DateFormatString = "dd/MM/yyyy";
                }
            );
    

【讨论】:

  • 在这里遇到了一个有趣的错误,我也遇到了与上面的答案相同的错误:"The property 'Model.SubmittedOn' is of type 'DateTime' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."
  • 这里的代码可能不是问题,但如果您知道会发生什么,我欢迎您提供任何帮助!
  • 让我知道您使用的是什么版本的 Postgres、Npgsql 和 Asp.Net 核心?这将有助于调试问题。我在使用 Postgres 10 和 12、Npgsql 2.1、2.2、4 和 Asp.Net Core 2.1 和 3.1 的 DateTime 方面从来没有遇到过任何问题。
【解决方案2】:

将数据库中的日期转换为不带时间的字符串,并将日期字符串转换为日期发送到数据库:

modelBuilder.Entity<Model>()
    .Property(e => e.SubmittedOn)
    .HasConversion(
        v => v.ToString("MM/dd/yyyy"),
        v => DateTime.Parse(v));

【讨论】:

  • 在这里遇到了一个有趣的错误,我也遇到了同样的错误,答案如下:"The property 'Model.SubmittedOn' is of type 'DateTime' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."
  • 这里的代码可能不是问题,但如果您知道会发生什么,我欢迎您提供任何帮助!
  • 很奇怪,你用的是什么数据库?
  • 我正在使用 postgres
  • 我不确定;没怎么用过postgres。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多