When I insert data with the following code, I see the exception. What should I do?
Code:
Movie_List_DBDataContext Movie_list1 = new Movie_List_DBDataContext();
Actor act = new Actor();
act.Actor_Name = Acttxt.Text;
Movie_list1.Actors.InsertOnSubmit(act);
Movie_list1.SubmitChanges();
Exception:
Violation of PRIMARY KEY constraint ‘PK_Actors’. Cannot insert duplicate key in object ‘dbo.Actors’.
My table has 2 columns; ID and Name, and ID is Primary Key.
In your
.dbmldesigner make sure that theIDfield is marked as “Auto Generated Value”. You can found this in the properties view of the field.Normally, this is initialized accordingly to the table in the database so, if the ID is set as an auto generated value in the database, the designer will automatically set the “Auto Generated Value” to true.
You can also mark the desired field as an “Auto Generated Value” in the code.
Search the
IDproperty in the generated code behind and set the value in theColumnattribute :IsDbGenerated=true