Get Error : The 'Id' property on 'Match' cou

本文关键字:on Match cou property Id Get Error The | 更新日期: 2023-09-27 18:02:29

我得到了这个错误:

无法将'Match'上的'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; }
}

Get Error : The 'Id' property on 'Match' cou

从消息中,几乎可以确定表的Id类型为System.int32 (int),类的Id类型为system.int64 (long)。

更改匹配。如果数据库字段是int(这里必须是这种情况),则Id转换为int。

根据@Nikhil Agrawal的建议,将您的MatchId转换为ToInt64

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

在。net中,long和Int64是相同的