store only date in database not time portion C#
Asked Answered
I

6

31

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

Ibanez answered 17/11, 2015 at 16:35 Comment(5)
Well, what is the datatype of the column in your database table?? If the column in your database is DATETIME - then of course you get date & time stored. The database column must be of datatype DATE - not just your attribute on the C# class!Caulk
make your database column a DATE !Caulk
Thats more a viewing problem then a storing problem. A datetime is a datetime even if the time portion is set to nothing. Your tool that is showing the content of the table just shows the default format for datetime. If you don't want to see the timepart change the setting of the tool used for viewing db content.Stoat
You can only make it DATE on the database level - in .NET and Entity Framework, we don't have a specific "date-only" datatype. In .NET code, you always have date&time - can't change that. But in the SQL Server database table, you can make it a DATE and store the date onlyCaulk
How your database is created ? If you follow Code First and generate your database from your code then you must add Column(TypeName="date") attribute on that property.Terza
L
61

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.

Lo answered 19/11, 2015 at 13:28 Comment(0)
P
4

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");
        }
Pare answered 23/3, 2020 at 5:24 Comment(1)
Is this tied to a specific DB engine?Pekoe
I
2

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

Intramolecular answered 15/11, 2021 at 20:6 Comment(0)
S
1

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");
Shanel answered 17/11, 2015 at 18:5 Comment(0)
S
-2

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,

Stroman answered 17/11, 2015 at 16:54 Comment(1)
Any particular reason to downvote? HE want store DATE ONLY in database and there is a DATE type which does exactly that lolStroman
C
-3

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();
Clemenceau answered 18/11, 2015 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.