小编She*_*erd的帖子

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

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

c# sql-server asp.net-core ef-core-2.2

5
推荐指数
1
解决办法
6532
查看次数

标签 统计

asp.net-core ×1

c# ×1

ef-core-2.2 ×1

sql-server ×1