1

microsoft sql server has time format for database column.

But how to configure it in entity framework code first?

I found DateTime but not Time

Refer to this link: http://msdn.microsoft.com/en-us/library/bb677243.aspx

eulercode
  • 1,107
  • 4
  • 16
  • 29
  • 1
    It's not at all clear what you mean, I'm afraid. Normally if you're storing a `DateTime` or `TimeSpan` value you shouldn't think of a *text* representation at all... – Jon Skeet Dec 18 '14 at 13:17
  • Dates and times have no format, they are types, stored as binary values. SQL Server has no format for the time type. Use TimeSpan in your EF code to map time to Timespan. The link you posted doesn't say anything different. Perhaps you are confusing date and time literals with the actual type implementation? – Panagiotis Kanavos Dec 18 '14 at 13:24
  • well, i guess timespan is the thing i am looking for. Is it better to store in timespan instead of datetime if i want to store time only? – eulercode Dec 18 '14 at 13:40
  • http://stackoverflow.com/q/2037283/150342 – Colin Dec 18 '14 at 14:28

2 Answers2

1

hi i solved this problem in this way

i add my time property to my class as date time type and add one migrations

that migration is like this

 CreateTable(
            "dbo.Events",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    EventName = c.String(nullable: false),
                    StartDate = c.DateTime(nullable: false, storeType: "date"),
                    EndDate = c.DateTime(nullable: false, storeType: "date"),
                    StartTime = c.Time(nullable: false),
                    EndTime = c.Time(nullable: false),
                    Description = c.String(),
                })
            .PrimaryKey(t => t.Id);

and after this i changed StartTime and EndTime type from Date time to time then you can run update-database command.

My Table pic from database

Mahdi Rahimi
  • 564
  • 4
  • 22
0

Use the DateTime datatype but ignore the Date and only view only the Time as desired. You could also use TimeSpans but I would say that the former option is still the easiest.

DateTime.Now.ToString("t");
  • This is a very bad idea, introducing formatting, internationalization *and* performance issues. There's no need to use string to save `date` and `time` data, .NET already has the equivalent DateTime and TimeSpan classes – Panagiotis Kanavos Dec 18 '14 at 13:22