KARAN
KARAN

Reputation: 1041

store only date in database not time portion C#

I have a test class and an ExecutionDate property which stores only date but when we use [DataType(DataType.Date)] that also stores the time portion in database but I want only date portion.

public class Test
{
     [Key]
     public int Id { get; set; }

     [DataType(DataType.Date)]
     public DateTime ExecutionDate { get; set; }      
}

Is any way to store only date on time portion in db using Entity Framework? Please help me....

I have added snapshot when use [DataType(DataType.Date)] that stores time portion 00:00 I want remove that

enter image description here

Upvotes: 31

Views: 43696

Answers (6)

Mateusz D
Mateusz D

Reputation: 348

In .NET 6 now you have new special type - DateOnly

More info here: https://github.com/dotnet/efcore/issues/24507 https://www.stevejgordon.co.uk/using-dateonly-and-timeonly-in-dotnet-6

Upvotes: 2

Lapenkov Vladimir
Lapenkov Vladimir

Reputation: 3218

On EF core one may add override OnModelCreating in DbContext class.

protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<Test>().
                Property(p => p.ExecutionDate)
                .HasColumnType("date");
        }

Upvotes: 4

Ravi M Patel
Ravi M Patel

Reputation: 3045

I think you are trying to specify database column type. You could use data annotations as described in this article.

Here is an example :

[Table("People")]
public class Person
{
    public int Id { get; set; }

    [Column(TypeName = "varchar")]
    public string Name { get; set; }

    [Column(TypeName="date")]
    public DateTime DOB { get; set; }
}

By default, string is translated to nvarchar, we have changed that here. Also Datetime (this is what you asked I suppose) which by default maps to datatime in sql server, is changed to date which stores only the date portion and not the time portion of a DateTime value.

Upvotes: 61

Krishna shidnekoppa
Krishna shidnekoppa

Reputation: 1059

Change DateTime to nvarchar(10) in database if u use DateTime in database 00.00.00 will be automatically assigned by database

string date = DateTime.Today.ToShortDateString();

Upvotes: -3

Mark Homer
Mark Homer

Reputation: 1035

Change datatype on the database from Datetime to Date type

Is it really an issue for you anyway? You may want time in there at some point,

Upvotes: -2

Ryan Intravia
Ryan Intravia

Reputation: 420

As David said above, once you bring that data into the application it will be a DateTime with the timestamp added onto the date. This would be the same result too if you stored the date as a string and used Convert to change to a DateTime for any manipulation:

string date = "2015-11-17";
var dateTime = Convert.ToDateTime(date);

Unless you want to manipulate strings in your application to avoid the timestamp, you can only work with DateTime. For display purposes though, you can always format the date and remove the timestamp:

var dateTime = DateTime.Now;
var formatDate = dateTime.ToString("yyyy-MM-dd");

Upvotes: 1

Related Questions