ASP yields a Violation of PRIMARY KEY constraint on a specific ID. (I randomly generate the ID's in code, while checking they don't already exist in the database).
When I debug this code on my local machine (with the same Azure hosted database) everything works fine. When I publish my code on Azure I get the error. Strange thing is that the random ID, yielded by the error, does NOT exist in my database.
Code (I copy a list of medication with a new id & new schemeID):
List<medication> l = db.medication.AsNoTracking().Where(x => x.scheme_ID == schemeID).ToList();
foreach (medication m in l) {
int id = generateUniqueMedicationID();
m.ID = id;
m.scheme_ID = newSchemeID;
db.medication.Add(m);
}
db.SaveChanges();
generateUniqueMedicationID() method:
private int generateUniqueMedicationID()
{
bool stop = true;
int id = -1;
while (stop)
{
Random r = new Random();
id = r.Next(100000000, 1000000000);
if (!db.medication.Any(o => o.ID == id)) stop = false;
}
return id;
}