【问题标题】:Change DateTime2 format in Entity Framework在实体框架中更改 DateTime2 格式
【发布时间】:2018-06-17 03:02:51
【问题描述】:

是否可以将默认的 DateTime2 格式从 ("MM/dd/yyyy") 更改为 ("dd/MM/yyyy")?

P/S:当我使用 SomeDate.ToString() 时,它会显示日期格式(“MM/dd/yyyy”)。我希望 DateTime 格式为 ("dd/MM/yyy") 而不使用 SomeDate.ToString("dd/MM/yyyy") 并在模型中设置日期格式。

【问题讨论】:

  • 参考以下链接:stackoverflow.com/questions/7674355/…>
  • stackoverflow.com/questions/7674355/…>参考以上链接
  • 日期时间不存储为字符串!在您的模型DateTime 属性上使用[DisplayFormat] 属性以您想要的格式显示它(与视图中的@Html.DisplayFor()@Html.EditorFor() 结合使用(或者只是将服务器上的文化更改为显示日期的文化) dd/MM/yyyy 格式)

标签: c# asp.net-mvc entity-framework


【解决方案1】:

您已创建 DbContext 以隐藏数据在数据库中的保存方式。我们 DbContext 的用户不必知道 DateTime 是保存为字符串,还是保存为 Ticks 的数量或任何方法。他们只需要知道 DbContext DateTime 值的准确性和限制(当不同于 System.DateTime 时)。

显然,您已将 DateTime 值建模为“DateTime2”。其效果是它们具有与 System.DateTime 相同的精度。

假设以下 DbContext 带有一个学生

class Student
{
    public int Id {get; set}
    public Datetime Birthday {get;set;}
}
class MyDbContext : DbContext
{
    public DbSet<Student> Students {get; set;}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // all DateTime are DateTime2
        modelBuilder.Properties<DateTime>().Configure(p => p.HasColumnType("datetime2"));
        ...
    }
}

现在如果这个 DbContext 的用户有一个学生的生日,它就是一个 System.DateTime。 DbContext 的用户不必了解内部数据库格式。我的建议是不要理会这种内部格式,以使将来的更改更容易。

如果您使用System.DateTime.ToString(),则结果可能不是您想要的格式。要更改此设置,您必须更改 CurrentCulture.CultureInfo,而不是数据库中使用的格式。

【讨论】:

    【解决方案2】:

    您的环境选择使用不带参数的ToString 时获得的格式。您必须定义格式并将其设置为环境。如果你不想弄乱你的系统,你可以为 DateTime 类编写扩展

    我写了一个扩展来使用不同的输出格式。

    文件名的反向日期字符串。用于文本输出的德语日期字符串..

    public static class DateTimeExtension
    {
        public static string ToReverseShortDateString(this DateTime date)
        {
            return date.ToString("yyyy-MM-dd");
        }
    
        public static string ToGermanDateString(this DateTime date)
        {
            return date.ToString("dd.MM.yyyy");
        }
    }
    

    如果您想了解更多关于现有类的扩展方法: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/extension-methods

    【讨论】:

      【解决方案3】:

      DateTime 值没有格式。它只代表日期和时间(在 ISO 日历中,可能在不同的时区,但那是另一回事)。它就像一个 int - 它不代表“十进制整数”或“十六进制整数” - 它只是特定范围内的整数。您可以将数字格式化为十进制或十六进制,但它本身没有格式。

      听起来你应该用 ParseExact 解析它以指定从文本框转换时的格式,或者可能是 TryParseExact:

      // This is assuming you're absolutely sure of the format used. This is *not*
      // necessarily the user's preferred format. You should think about where your
      // data is coming from.
      DateTime date;
      if (DateTime.TryParseExact(text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                 DateTimeStyles.None, out date))
      {
          // Okay, successful parse. We now have the date. Use it, avoiding formatting
          // it back to a string for as long as possible.
      }
      

      出于所有目的,您应该将该值保留为 DateTime,除非将其返回给用户 - 此时您可能希望使用他们的文化设置。

      特别是,如果您将值存储在数据库中,则不应将其转换为文本并将其包含在 SQL 语句中 - 这是自找麻烦。相反,使用参数化 SQL 语句并将其设置为参数值,仍为 DateTime。

      【讨论】:

        【解决方案4】:

        DateTime2 不像您暗示的那样保存为字符串文字,它只是 DateTime2 范围内的数值。需要进行转换才能读取它,因为它可能类似于数据库中的 219,338,580,000,000,000,以纳秒长的形式保存。无论如何,除非您想对字符串进行资源较少的转换并将其保存为字符串并在使用时转换回日期时间,否则您将不得不对其进行转换。

        在您的模型中使用它,我建议将其用作 DateTime 对象并在需要时将其转换为 ToString。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多