Entity Framework: map varchar for DateTime property

Entity Framework 4. MySQL

I am trying to map varchar columns to a DateTime class property

my class:

[Table("webnews_in")] public class WEBNews_in : AbsNews { private DateTime _inDateTimeAdded = DateTime.MinValue; public DateTime InDateTimeAdded { get { return _inDateTimeAdded; } set { _inDateTimeAdded = value; } } private DateTime _inDateTimeUpdated = DateTime.MinValue; public DateTime InDateTimeUpdated { get { return _inDateTimeUpdated; } set { _inDateTimeUpdated = value; } } } 

 CREATE TABLE webnews_in ( Id INT NOT NULL auto_increment, VPN_AC VARCHAR (20) NULL, InDateTimeAdded VARCHAR (50) NULL, InDateTimeUpdated VARCHAR (50) NULL PRIMARY KEY (Id) ); 

I got this error:

  Input string was not in a correct format. Message Input string was not in a correct format. Data IDictionary (0 items) StackTrace at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at MySql.Data.Types.MySqlDateTime.ParseMySql(String s) at MySql.Data.MySqlClient.MySqlDataReader.GetDateTime(Int32 i) at MySql.Data.Entity.EFMySqlDataReader.GetDateTime(Int32 ordinal) HelpLink null Source mscorlib 
0
c # mysql orm entity-framework
source share
1 answer

EF throws an error every time you set the type in a model that is different from the table, i.e. if you set the field as a whole in the model, but this field has a row type in the table, then in the data load it will give you an error message. To avoid this, you should make a workaround by declaring the public property as an equivalent table type and converting it to the desired model type, try something like this:

 [Table("webnews_in")] public class WEBNews_in : AbsNews { private DateTime _inDateTimeAdded = DateTime.MinValue; public string InDateTimeAdded { get { return Format(_inDateTimeAdded, " dd/MM/yyyy hh:mm:ss tt"); } set { _inDateTimeAdded = DateTime.Parse(value); } } private DateTime _inDateTimeUpdated = DateTime.MinValue; public string InDateTimeUpdated { get { return Format(_inDateTimeUpdated, " dd/MM/yyyy hh:mm:ss tt"); } set { _inDateTimeUpdated = DateTime.Parse(value); } } } 
+2
source share

All Articles