简体   繁体   中英

How to change automatically assigned DateTime2 data type to DateTime with EF Core, Code First?

I am writing a new application with ASP.NET Core 2.2.4 with EF Core Code First approach with migrations. I wrote my domain models and updated the database via Package Manager Console but this resulted in all the DateTime properties I have throughout my domain models to be created as DateTime2(7) table columns. I need only DateTime.

First I tried to change this by writing a query in SQL server to change the data type and it did, inside SQL Server it's showing that it's changed to datetime. But when I open SQL Server Object Explorer in Visual Studio it still shows the column as a datetime2(7) type.

After that I tried adding the following annotation to the DateTime properties [Column(TypeName = "DateTime")]. I added a migration after that and updated the db via Package Manager Console. This didn't have any effect.

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; }

    }
}

Make sure you install Microsoft.EntityFrameworkCore.SqlServer . Then use the TypeName attribute:

[Column(TypeName = "datetime")]

If SQL Server Object Explorer is still showing datetime2, you may need to exit and re-open Visual Studio.

This answer doesn't provide a how to force EF Core to create a column with datetime instead of datetime2 because you shouldn't.

The main difference between SQL server datetime2 and datetime is the precision and wider date range that can be utilized. The default will be datetime2 for a System.DateTime and there isn't a reason not to use it when you are using a compatible client.

System.DateTime maps directly to SQL Server datetime2 and is the Microsoft recommended datatype for storage of a System.DateTime in SQL Server.

Also worth noting that a datetime2(7) takes one less byte of space than a datetime and offers increased precision.

See the Note on MSDN SQL Server datetime

What I did was add datetime type on the column in the migration like:

CreatedDate = table.Column<DateTime>(nullable: false, type: "datetime")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM