代码之家  ›  专栏  ›  技术社区  ›  Scott 混合理论 Alex F

对象必须实现IConvertable

  •  9
  • Scott 混合理论 Alex F  · 技术社区  · 12 年前

    描述:在执行期间发生未处理的异常 当前web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息以及错误在代码中的来源。

    异常详细信息:System.InvalidCastException:对象必须实现 I可转换。

    源错误:

    Line 20:         public ActionResult Index()
    Line 21:         {
    Line 22:             var feeds = (from f in _db.AlertRules.Include("AlertRuleOutages")
    Line 23:                          select f).ToList();
    Line 24: 
    

    堆栈跟踪:

    [InvalidCastException: Object must implement IConvertible.]
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +9508841
       MySql.Data.Entity.EFMySqlDataReader.ChangeType(Object sourceValue, Type targetType) +376
       MySql.Data.Entity.EFMySqlDataReader.GetValue(Int32 ordinal) +129
       System.Data.Common.Internal.Materialization.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) +215
       lambda_method(Closure , Shaper ) +468
       System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly(Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) +218
       lambda_method(Closure , Shaper ) +218
       System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +170
       System.Data.Common.Internal.Materialization.RowNestedResultEnumerator.MoveNext() +235
       System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.TryReadToNextElement() +16
       System.Data.Common.Internal.Materialization.ObjectQueryNestedEnumerator.MoveNext() +57
       System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
       System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
    

    数据库DDL:

    CREATE TABLE alertruleoutage
    (
    Id        INT NOT NULL auto_increment,
    RuleId    INT NOT NULL,
    StartTime TIME NULL DEFAULT 00:00:00,
    EndTime   TIME NULL DEFAULT 23:59:59,
    WeekDays  TINYINT NULL,
    Count     INT NULL,
    TimeFrame TIME NULL DEFAULT 01:00:00,
    PRIMARY KEY (Id),
    KEY RuleId (RuleId),
    CONSTRAINT RuleId FOREIGN KEY (RuleId) REFERENCES alertrule (Id)
    );
    
    
    
    CREATE TABLE alertrule
        (
        Id             INT NOT NULL auto_increment,
        FeedId         INT NOT NULL,
        TemplateId     INT NOT NULL,
        Enable         BIT (1) NULL DEFAULT b'1',
        `Rule`         VARCHAR (250) NULL,
        UpdateDateTime TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
        PRIMARY KEY (Id),
        KEY AlertFeedId (FeedId),
        KEY AlertTemplateId (TemplateId),
        CONSTRAINT FK_AlertFeedId FOREIGN KEY (FeedId) REFERENCES feed (Id),
        CONSTRAINT FK_AlertTemplateId FOREIGN KEY (TemplateId) REFERENCES alertruletemplate (Id)
        );
    

    POCO模型:

     [Table("AlertRuleOutage")]
    public class AlertRuleOutage
    {
        [Key]
        public int Id { get; set; }
    
        public int RuleId { get; set; }
    
        public DateTime? StartTime { get; set; }
    
        public DateTime? EndTime { get; set; }
    
        //public Week WeekDays { get; set; }
    
        public int? Count { get; set; }
    
        public DateTime? TimeFrame { get; set; }
    
        public virtual AlertRule Rule { get; set; }
    }
    
        [Table("AlertRule")]
    public class AlertRule
    {
        [Key]
        public int Id { get; set; }
    
        public int FeedId { get; set; }
    
        public int TemplateId { get; set; }
    
        public bool? Enable { get; set; }
    
        public string Rule { get; set; }
    
        public DateTime? UpdateDateTime { get; set; }
    
        public virtual Feed Feed { get; set; }
    
        public virtual AlertRuleTemplate Template { get; set; }
    
        public virtual List<AlertRuleOutage> AlertRuleOutages { get; set; }
    }
    

    数据库上下文:

        public DbSet<AlertRule> AlertRules { get; set; }
    
        public DbSet<AlertRuleOutage> AlertRuleOutages { get; set; }
    
    1 回复  |  直到 12 年前
        1
  •  7
  •   Scott 混合理论 Alex F    12 年前

    中的数据类型错误 AlertRuleOutage

    数据库 Time 应映射到.NET TimeSpan 以下为:

    public TimeSpan? StartTime { get; set; }
    

    另请参阅: http://www.devart.com/dotconnect/mysql/docs/DataTypeMapping.html