There are two relationed tables
When want use the following code to update the record in table cat_user, it will meet an Exception:
System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException
var cu = dc.cat_users.FirstOrDefault(c => c.uid == can.uid);
if (list_cat.Visible == true && cu.category.levels == 1 || cu.category.levels == 2)
{
int selectedvalue = Convert.ToInt32(list_cat.SelectedValue);
cu.cid = selectedvalue;
}
Change the code like following, it will update successful
var cu = dc.cat_users.FirstOrDefault(c => c.uid == can.uid);
if (list_cat.Visible == true && cu.category.levels == 1 || cu.category.levels == 2)
{
int selectedvalue = Convert.ToInt32(list_cat.SelectedValue);
cu.category = dc.categories.Single(c=>c.cid == selectedvalue);
}