Scaffolder creates DATETIME instead of DATE, even when explicitly set to DATE by data annotations

StackOverflow https://stackoverflow.com/questions/18022937

Frage

I have encountered a nuisance where the MVC scaffolder sets the data type in the database to DATETIME when I in my model have set the datatype to DATE using data annotations. Any clues why?

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

I am using VS2012, MVC 4.5.

Thanks!

War es hilfreich?

Lösung

I believe the DataTypeAttribute makes sense only in regards of the UI representation of the field (when using the helper methods DisplayFor and EditorFor, for example).

Quoting from MSDN (highlight is mine):

The DataTypeAttribute attribute lets you mark fields by using a type that is more specific than the database intrinsic types. For example, a string data field that contains e-mail addresses can be attributed with the EmailAddress type. This information can be accessed by the field templates and modify how the data field is processed.

It seems there is no reference to the actual database type used.

EDIT: Fluent Mapping?

Apparently EF also has the option of setting the DataType for a given property: msdn.microsoft.com/en-us/data/jj591617.aspx#1.10. I honestly never used it so if you wish to investigate and edit this answer with your findings it would be great! :)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top