【问题标题】:Data types in ASP.NET Core 3.1 MVC modelsASP.NET Core 3.1 MVC 模型中的数据类型
【发布时间】:2020-12-03 05:05:11
【问题描述】:

我对编码非常陌生,遇到了一个问题。我正在做一个 ASP.NET Core 3.1 MVC 应用程序。我正在尝试使用日期列创建模型,但没有任何成功。我使用的代码如下所示。

我的想法是必须有一种方法可以在 SQL 中存储日期,但无济于事。非常感谢任何对话或 cmet。

错误发生在这一行

public Date CreationDate { get; set; }

它不会接受 Date 作为数据类型。

问候史蒂夫

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace SalesAccount.Models
{
    public class Contract
    {
        [Key]
        public int Id { get; set; }
        [Required]
        **public Date CreationDate { get; set; }**
        [Required]
        public char ContractNumber { get; set; }
        
    }
}

【问题讨论】:

  • .NET / C# NOT 有一个名为 Date 的数据类型 - 您始终需要使用 DateTime(或 DateTimeOffset,如果时区信息相关)

标签: sql model-view-controller types


【解决方案1】:

为此目的使用数据类型DateTime 而不是Date。在 C# 中,Date 数据类型不存在。

public DateTime CreationDate { get; set; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2018-06-21
    • 2014-01-07
    • 2012-08-09
    相关资源
    最近更新 更多