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; }
}
}
3条答案
按热度按时间hfyxw5xn1#
This answer doesn't provide a how to force EF Core to create a column with
datetime
instead ofdatetime2
because you shouldn't.The main difference between SQL server
datetime2
anddatetime
is the precision and wider date range that can be utilized. The default will bedatetime2
for aSystem.DateTime
and there isn't a reason not to use it when you are using a compatible client.System.DateTime
maps directly to SQL Serverdatetime2
and is the Microsoft recommended datatype for storage of aSystem.DateTime
in SQL Server.Also worth noting that a
datetime2(7)
takes one less byte of space than adatetime
and offers increased precision.See the Note on MSDN SQL Server datetime
eqfvzcg82#
Make sure you install
Microsoft.EntityFrameworkCore.SqlServer
. Then use theTypeName
attribute:If SQL Server Object Explorer is still showing datetime2, you may need to exit and re-open Visual Studio.
du7egjpx3#
What I did was add datetime type on the column in the migration like: