I Have a model like this
public partial class TableNames
{
public string Name { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int IntId { get; set; }
}
Then in a controller I'm Trying to get max IntIdfrom that model
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);
When the table has no records (it's empty) , the controller throws this error
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.
How can I do to fix it ?