我正在使用 ASP.NET Core 2.2.4 和 EF Core Code First 迁移方法编写一个新应用程序。我编写了域模型并通过包管理器控制台更新了数据库,但这导致我在整个域模型中拥有的所有 DateTime 属性都被创建为 DateTime2(7) 表列。我只需要日期时间。
首先,我尝试通过在 SQL Server 中编写查询来更改数据类型来更改此设置,结果确实如此,在 SQL Server 内部显示它已更改为日期时间。但是当我在 Visual Studio 中打开 SQL Server 对象资源管理器时,它仍然将该列显示为 datetime2(7) 类型。
之后,我尝试将以下注释添加到 DateTime 属性 [Column(TypeName = "DateTime")]。之后我添加了迁移并通过包管理器控制台更新了数据库。这没有任何效果。
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 = …Run Code Online (Sandbox Code Playgroud)