0

实体框架 4。MySQL

我正在尝试将varchar 列映射到类的DateTime属性

我的课:

[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)
    );

我收到了这个错误:

    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 
4

1 回答 1

2

每次在模型中设置与表不同的类型时,EF都会抛出错误,即,如果您在模型中将字段设置为整数,但该字段在表中具有字符串类型,则在加载数据时它会给你一个错误。为避免这种情况,您必须采取一种解决方法,声明一个具有等效表类型的公共属性并将其转换为模型所需的类型,请尝试以下操作:

[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);
       }
   }
}
于 2012-08-17T03:53:35.893 回答