2011-03-31 6 views
3

LINQ to SQL을 사용하여 잘못된 캐스트 예외가 발생했습니다.LINQ to SQL의 캐스트가 유효하지 않습니다.

[Table(Name = "tbl_request$")] 
public class request 
{ 
    [Column(IsPrimaryKey = true)] 
    public float requestID; 
    [Column] 
    public string moduleCode; 
    [Column] 
    public float park; 
    [Column] 
    public string requestedRoom; 
    [Column] 
    public float week; 
    [Column] 
    public float day; 
    [Column] 
    public float period; 
    [Column] 
    public float status; 
    [Column] 
    public float semester; 
    [Column] 
    public float priority; 
    [Column] 
    public float adhoc; 
    [Column] 
    public float numStudents; 
    [Column] 
    public float collectionID; 
    [Column] 
    public float roomCount; 
} 

이 해당하는 데이터베이스 테이블을

requestID float Unchecked 
moduleCode nvarchar(255) Checked 
park float Checked 
requestedRoom nvarchar(255) Checked 
week float Checked 
day float Checked 
period float Checked 
status float Checked 
semester float Checked 
priority float Checked 
adhoc float Checked 
numStudents float Checked 
collectionID float Checked 
roomCount float Checked 

모두가 나에게 잘 보이는

에 : 여기에 C#에서 내 수업입니다. 다른 제안?

Specified cast is not valid. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Specified cast is not valid. 

Source Error: 

Line 12:    var bookingtable = Database.DatabaseContext.GetTable<Database.request>(); 
Line 13:    var dict = new System.Collections.Generic.Dictionary<int, System.Collections.Generic.List<Database.request>>(); 
Line 14:    foreach (var request in bookingtable) 
Line 15:    { 
Line 16:     if (dict[(int)request.collectionID] == null) 

스택 추적은 LINQ 내부 깊숙한 곳입니다.

+0

예외를 게시 할 수 있습니까? –

+0

@Daniel : 불행히도 "Invalid Cast"는 말 그대로 거의 틀림없이 말입니다. – Puppy

+0

어떤 개체가 잘못 캐스팅되었는지 InnerException을 체크 인하십시오. –

답변

5

DB 유형이 FLOAT이더라도 C#은 float? 대신 double?을 원했습니다.

+0

에 'float?'테이블을 드래그 앤 드롭하면됩니다. 물론'float? '도 작동해야하지만, 두배? '. 귀하의 경우, 아마도 그냥 - 잊어 버린 부분, 데이터베이스의 필드 NULL이 될 수 있습니다 – skarmats

+2

Btw, 나는 항상 비주얼 디자이너를 사용하여 모델 클래스를 만드는 것이 좋습니다. 어색한 것처럼 보일지 모르지만, 특히 트랙과 같은 고급 주제로 이동하면 많은 오류를 추적하지 않아도됩니다. 테이블에 외래 키를 정의하고 끌어서 놓기 만하면 코드에서 올바른 연관성을 갖게됩니다. 디자이너 클래스에 사용자 정의 비헤이비어를 추가하려면 이러한 추가 사항을 별도의 클래스 파일에 넣고 클래스를 부분 클래스로 만듭니다. 키워드 Linq-to-Sql, 부분 클래스를 검색하면 지침이 있습니다 – skarmats

+2

알림 : SQL 서버 *의 플로트는 4 바이트 저장 크기 일 수 있습니다.이 경우 aC# float (32 비트)는 아마 작업. 그러나 지정된 숫자 정밀도가 24 인 SQL Server 부동 소수점은 8 바이트 저장소 크기가되고, C# 코드에서 이중 (64 비트)을 사용하지 않으면 오류가 발생합니다. 방금 같은 문제가 발생하여 Nullable 표시기를 잊어 버린 것과 관련이 없습니다. – ptrandem