I am terrible with databases, so please carry me.
I have a program that gets some user information and adds it to the database table. Then I will need to get additional information for this table and update it. For this, I tried to do this:
public static void updateInfo(string ID, string email, bool pub)
{
try
{
MyDataDataContext db = GetNewDataContext();
User user = db.Users.SingleOrDefault(x => x.UserId == long.Parse(ID));
if (user != null)
{
user.Email = email;
user.Publish = publish;
}
db.Users.InsertOnSubmit(user);
db.SubmitChanges();
}
catch (Exception ex)
{
Log(ex.ToString());
}
}
But I get this error:
System.InvalidOperationException: Unable to add an entity that already exists.
I know this because I already have an entry in the table, but I donβt know how to edit the code for the update, and not try to create a new one?
Why is this not working? Wouldnβt send changes to the current update item of this item and create a new one?