Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL 'time' type in Entity Framework Code First

I'm trying to create a 'time(7)' column in a table with Entity Framework Code First. This is my Entity:

public class ShiftDetail {     public long Id { get; set; }      [Required]     public int DayOfWeek { get; set; }      [Required]     [Column(TypeName="time")]     public DateTime StartTime { get; set; }      [Required]     [Column(TypeName = "time")]     public DateTime EndTime { get; set; }      public long ShiftId { get; set; }     public virtual Shift Shift { get; set; } } 

As you can see I'm trying to set the database type for the columns StartTime and EndTime to "time" but I get this error:

(112,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.DateTime[Nullable=False,DefaultValue=,Precision=]' of member 'StartTime' in type 'ShiftDetail' is not compatible with 'SqlServer.time[Nullable=False,DefaultValue=,Precision=7]' of member 'StartTime' in type 'CodeFirstDatabaseSchema.ShiftDetail'.

I've tried also with TypeName="time(7)" but I get this other error:

(104,6) : error 0040: The Type time(7) is not qualified with a namespace or alias. Only primitive types can be used without qualification.

How can I create a time column with code first? (preferably without fluent API)

Thanks in advance.

like image 333
Escobar5 Avatar asked Aug 29 '12 20:08

Escobar5


People also ask

How does Entity Framework store time?

If you want to use Time type in database you will have to use TimeSpan with 24 hour cycle in your application. DateTime is not representation of time. Actually, when you want to store time of day, there are two good reasons to use DateTime in entities. .

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…


1 Answers

If you want to use Time type in database you will have to use TimeSpan with 24 hour cycle in your application. DateTime is not representation of time.

like image 80
Ladislav Mrnka Avatar answered Sep 28 '22 06:09

Ladislav Mrnka