2015-07-13 41 views
0

我得到這個錯誤:得到錯誤:在「匹配」的「ID」屬性不能被設置爲「System.Int32」值

上「匹配」不能在「ID」屬性設置爲'System.Int32' 值。您必須將此屬性設置爲 'System.Int64'類型的非空值。 「

一個代碼片斷,如:

Match _match =_entities.Match.SingleOrDefualt(match => match.Id == MatchId); 

它發生有時並不是所有的時間和我檢查我的數據庫POCO類的類型,它是正確的 我使用EF 6.1.3代碼首先我! !很困惑

我的實體類是:

[Table("Match")] 
public class Match 
{ 
    public Match() 
    { 
     Hands = new HashSet<Hand>(); 
     Rounds = new HashSet<Round>(); 
    } 

    public long Id { get; set; } 

    public DateTime CreationDate { get; set; } 

    public long FirstPlayerId { get; set; } 

    public byte FirstPlayerRedrawHandCount { get; set; } 

    public long? SecondPlayerId { get; set; } 

    public byte SecondPlayerRedrawHandCount { get; set; } 

    public byte SpeedType { get; set; } 

    public MatchType MatchType { get; set; } 

    public byte FirstPlayerScore { get; set; } 

    public byte SecondPlayerScore { get; set; } 

    public MatchStatus Status { get; set; } 

    public long? DCUser { get; set; } 

    public DateTime? RetryTime { get; set; } 

    [Timestamp] 
    public byte[] RowVersion { get; set; } 

    public RegisterState RegisterState { get; set; } 

    [ForeignKey("FirstPlayerId")] 
    public virtual User User { get; set; } 
    [ForeignKey("SecondPlayerId")] 
    public virtual User User1 { get; set; } 

    public virtual ICollection<Hand> Hands { get; set; } 

    public virtual ICollection<Round> Rounds { get; set; } 
} 
+1

什麼是'MatchId'在'比賽_match = _entities.Match。 SingleOrDefualt(match => match.Id == MatchId);'?這是從哪裏來的?顯示更多的代碼。 –

+2

也許'match.Id == Convert.ToInt64(MatchId)' –

+0

@NikhilAgrawal,這不是問題。錯誤消息是關於分配,而不是比較。問題必須是實體在Match實例中試圖將數據放在表上時。 – Jauch

回答

1

從MESSA ge,幾乎可以肯定你的表有一個System.int32(int)類型的Id,而你的類有一個system.int64(long)類型。

如果數據庫字段是int,則將Match.Id更改爲int(這裏必須是這種情況)。

+0

我提到數據庫的列和類的類型都很長(int int) –

+0

確實。但是錯誤在於你正在試圖詮釋一個整數。如果錯誤出現在你顯示的行上,那麼你需要在數據庫中有一個int類型的matxh類。你正在使用哪個數據庫? SQL服務器? – Jauch

+0

雅我使用SQL Server 2014年,我應該再次說,它發生了一些時間,我感到困惑 –

0

至於建議的@Nikhil阿格拉瓦爾,將您MatchId到ToInt64

Match _match =_entities.Match.SingleOrDefualt(match => match.Id == Convert.ToInt64(MatchId)); 

由於在.NET中,長,Int64的相同

+0

MatchId很長​​!我應該再次轉換它嗎? –

相關問題