如何在EF Core,Code First中将自动分配的DateTime2数据类型更改为DateTime?

5

我正在使用ASP.NET Core 2.2.4和EF Core的Code First方法来编写新应用程序并进行迁移。我编写了领域模型并通过Package Manager Console更新了数据库,但结果是在我的所有领域模型中的DateTime属性都被创建为DateTime2(7)表列。

我只需要DateTime。

首先,我尝试通过在SQL Server中编写查询来更改数据类型,它确实更改了数据类型,在SQL Server内部显示为datetime。但是,当我在Visual Studio中打开SQL Server Object Explorer时,仍然显示该列为datetime2(7)类型。

之后,我尝试在DateTime属性中添加以下注释[Column(TypeName =“DateTime”)]。接着我执行了迁移操作,并通过Package Manager Console更新了数据库。然而,这没有任何效果。

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace GYM.CoreApp.WebUI.Models
{
    public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }

        [Required]
        [MaxLength(100), MinLength(2)]
        [Display(Name ="First Name")]
        public string FirstName { get; set; }

        [Required]
        [MaxLength(100), MinLength(2)]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required]
        [Column(TypeName = "DateTime")]
        [Display(Name = "Date of birth")]
        public DateTime DateOfBirth { get; set; }

        [Required]
        [MaxLength(13), MinLength(13)]
        [Display(Name = "EMBG")]
        public string EMBG { get; set; }

        [Required]
        [MaxLength(200), MinLength(2)]
        [Display(Name = "Home address")]
        public string Address { get; set; }

        [Required]
        [MaxLength(13), MinLength(9)]
        [Display(Name = "Phone number")]
        public string PhoneNumber { get; set; }

        [Required]
        [Display(Name = "Salary per month")]
        public int SalaryPerMonth { get; set; }

        [Required]
        [Column(TypeName = "DateTime")]
        [Display(Name = "Date when joined company")]
        public DateTime DateWhenJoined { get; set; }

        [Required]
        [Display(Name = "Still Employed")]
        public bool StillEmployed { get; set; }

    }
}

[Column(TypeName = "DateTime")]应该可以解决问题,你可能看到了一些缓存的数据库信息。话虽如此,我更希望列的类型是Date - Gert Arnold
[Column(TypeName = "DateTime")]应该可以解决问题,你可能看到了一些缓存的数据库信息。话虽如此,我更希望列的类型是Date - undefined
3个回答

7

这个答案没有提供如何强制EF Core创建一个使用datetime而不是datetime2列的方法,因为你不应该这样做。

SQL Server datetime2datetime之间的主要区别在于精度和可使用的更广泛的日期范围。对于System.DateTime,默认情况下将使用datetime2,在使用兼容客户端时没有理由不使用它。

System.DateTime直接映射到SQL Server datetime2,是Microsoft推荐的在SQL Server中存储System.DateTime的数据类型。

值得注意的是,datetime2(7)所占用的空间比datetime少一字节,并提供了更高的精度。

请参阅MSDN SQL Server datetime上的注释。


这很好知道。然而,我是从用户的角度出发。没有用户会愿意填写整个日期时间格式。你会如何执行这个? - Shepherd
请提供一个代码示例,说明您所说的“完整日期时间格式”是什么。在您的C#代码中,使用System.DateTime存储到SQL服务器作为datetime2时,并没有什么不同。 - b_stil
@Shepherd 用户界面应该完全独立于底层存储机制。正如在这个答案中所指出的那样,.net存储是相同的,因此用户界面永远不会知道。 - Dale K
我的意思是通过拥有一个输入字段,例如: <input type="date" name="bday"> - Shepherd
你的“input”显示与数据库存储类型无关。它显示为这样,是因为你选择了一个“date”输入类型。使用日期/时间选择器,忘记存储机制。 - Dale K
显示剩余2条评论

2
确保安装了Microsoft.EntityFrameworkCore.SqlServer。然后使用TypeName属性:
[Column(TypeName = "datetime")]

如果SQL Server对象浏览器仍然显示datetime2,您可能需要退出并重新打开Visual Studio。"最初的回答"

已安装。它是2.2版本。如果字符串是“datetime”还是“DateTime”,有区别吗? - Shepherd
不确定。试试看!另外,如果您的数据库现在已经正确使用日期时间,并且只是Visual Studio出现问题,请尝试关闭并重新打开Visual Studio。 - John-Luke Laue
1
仍在创建datetime2(7)。 - liqSTAR

0
我所做的是在迁移中的列上添加了datetime类型。
CreatedDate = table.Column<DateTime>(nullable: false, type: "datetime")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接