我有一个这样的模型
public partial class TableNames
{
public string Name { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int IntId { get; set; }
}
然后在控制器中,我试图从该模型中获取最大 IntId
var max = from c in db.TableNames
select c;
int? Max = max.AsQueryable().Max(x => x.IntId); //This isntruction throws an error
int IntId = ( Max == null ? 1 : Max + 1);
当表没有记录(它是空的)时,控制器会抛出这个错误
The cast to value type 'Int32' failed because the materialized value is null.
Either the result type's generic parameter or the query must use a nullable type.
我该怎么做才能解决它?