指定的强制转换在读取时无效,但插入有效吗?

时间:2014-06-16 05:38:19

标签: c# sql database linq linq-to-sql

我有一个SQL表(见下文),我有两个类:IncidentCircuitIncidentCircuitIncident派生自事件,并使用trace_id引用(FK)和枚举-1,0,1,2的严重性枚举进行扩展。

使用Linq to SQL我能够在表中存储 CircuitIncident。但是,当使用linq读取表时,我在“foreach”行中得到以下异常:

var result = from eachIncident in db.Incidents.OfType<CircuitIncident>()
             where selectedTraceItems.Select(t => t.Id).Contains(eachIncident.CircuitTraceId)
             select eachIncident;

foreach(CircuitIncident circInc in result)
{
   //do something
}
  

测试方法SmartVisionUnitTests.IncidentViewModelTest.testLoad引发异常:System.InvalidCastException:指定的强制转换无效..

我已经交叉检查了类型等等,但我不知道为什么我能够存储,但不能读取数据。有人知道为什么吗?或者有人知道如何进一步缩小根本原因吗?

这是我的表定义:

CREATE TABLE dbo.incident_tab 
( 
    [ID] [int] IDENTITY(1,1) NOT NULL, 
    START real NOT NULL, 
    STOP real NOT NULL, 
    POS real NOT NULL, 
    SEVERITY smallint, 
    RACE_ID int NULL, 
    TimeStamp datetime NOT NULL, 
    TYPE char(1), 
    CONSTRAINT PK_incident_tab PRIMARY KEY CLUSTERED ([ID] ASC) 
) ; 

ALTER TABLE dbo.incident_tab ADD CONSTRAINT FK_INCIDENT_TRACE_ID FOREIGN KEY (TRACE_ID) REFERENCES dbo.trace_tab (ID) ; 

堆栈跟踪:

System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo,   IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
System.Linq.IQueryProvider.Execute[TResult](Expression expression)
System.Linq.Queryable.First[TSource](IQueryable`1 source)
SmartVisionUnitTests.IncidentViewModelTest.testLoad() in Z:\SmartVision\SmartVision\SmartVisionTest\IncidentViewModelTest.cs: line 87


[Table(Name = "incident_tab")]
[InheritanceMapping(Code = "C", Type = typeof(CircuitIncident), IsDefault = true)]
public class Incident 
{
    public int Number { get; set; }

    [Column(IsDiscriminator = true)]
    private string TYPE;

    [Column(Name="POS")]
    public float Position { get; set; }

    public virtual float Size
    {
        get { return IncidentStop - IncidentStart; }
    }

    [Column(Name = "START")]
    public float IncidentStart { get; set; }

    [Column(Name = "STOP")]
    public float IncidentStop { get; set; }

    [Column(Name = "TIMESTAMP")]
    public virtual DateTime Date { get; set; }

    [Column(Name = "ID", IsPrimaryKey = true, IsDbGenerated = true)]
    private int id;
    public int Id
    {
        get { return id; }
        set { id = value; }
    }
}



public class CircuitIncident : Incident
{
    private int circuitTraceId = 0;

    [Column(Name="TRACE_ID")]
    public int CircuitTraceId
    {
        get { return circuitTraceId; }
        set { circuitTraceId = value; }
    }

    private AlarmStatusType severity = AlarmStatusType.Unknown;
    [Column(Name = "SEVERITY")]
    public AlarmStatusType Severity
    {
        get { return severity; }
        set { severity = value; }
    }
}

谢谢和问候。

安德烈亚斯

1 个答案:

答案 0 :(得分:0)

好的,我自己解决了 - &gt;我试图将枚举映射到DB smallint字段。如果我在DB上使用“int”它可以工作。仍然扼杀我,因为根据Microsoft文档,应该可以将枚举映射到smallint。