我正在使用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