It seems like you want to initialize the ProjetCount
property with a value of 0 if the table is empty. To achieve this, you can modify your IncrementIdAsync
method to handle the case when the table is empty. You can check if there are any records in the table and conditionally set the value of ProjetCount
. Here's how you can modify your code:
// Take the number
private async Task<ProjetCount> IncrementIdAsync()
{
int max = 0;
if (await _context.ProjetModels.AnyAsync())
{
max = await _context.ProjetModels.MaxAsync(d => d.ProjetCount) + 1;
}
return new ProjetCount() { ProjCount = max };
}
In this modified code:
We check if there are any records in the ProjetModels
table using _context.ProjetModels.AnyAsync()
. If there are no records, max
will remain 0, which means the table is empty.
If there are records in the table, we calculate the maximum value for ProjetCount
and increment it by 1.
Finally, we return a ProjetCount
instance with the calculated value of max
, which will be 0 if the table is empty or the next value if there are existing records.
With this change, ProjetCount
will be initialized to 0 when the table is empty, and it will be incremented properly for new records.