I want to save data if the data is 0 and update the data when it already have the data.
This is my code:
foreach (var monthly in entry.Value)
{
SaleSummary TblSale = new();
SaleSummary sales = new();
int year = monthly.Key / 12;
int month = monthly.Key % 12;
sales.ID = entry.Key;
sales.totalSales = monthly.Value;
sales.MonthlySales = month;
sales.YearlySales = year;
if (sales.ID == 0)//i think this is the issue(?)
{
Repository.repoMandays.Save(sales);
}
else
{
Repository.repoMandays.Update(sales);
}
}
The problem here is when the program start it keep adding more data and not replacing the old one.
I don't know the if else is right or not, and how to improve the code?
I'm trying to make some program to save to database when it is empty and update the old data with the new one when the data already exist, but my code doesn't seem right.