problems add new record on entity framework
-
Hello ! i have a sql server 2008r2 database ( stu) with a table (st) with these columns : stID - identity (int autoincrement value) name - char(10) com - char(20) i have a project in vb.net 2010 , and i have generated entity framework model with the database. I have a form with a listbox and 2 textbox , and 2 buttons Addrecord and Saverecord. The listbox is bound with an object from entity (stBindingSource) and displayMember: name , Valuememeber : stID. The form is also binded with stbindingsource. ( textbox1:name , Textbox2: com) I want to add new record.The code is : ……----------------------------------- Dim stu As stuEntities Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load stu= New stuEntities stBindingSource.DataSource = stu.sts End Sub --------------------------------------------- Private Sub Addrecord_Click(sender As System.Object, e As System.EventArgs) Handles Addrecord.Click stBindingSource.AddNew() End Sub ------------------------------------------------- Private Sub Saverecord_Click(sender As System.Object, e As System.EventArgs) Handles Saverecord.Click stBindingSource.EndEdit stu.SaveChanges(System.Data.Objects.SaveOptions.None) End Sub So ,first i click to "Addrecord" , and after i fill the 2 textbox , i click Saverecord. The first record is saved with no problem.If a add another record , the record is saved , but when I open the table on database , the first record is saved 2 times , the second record is saved 1 time. I try to add 3,4… records and everytime the last record is saved 1 time the others are all duplicated If I call stu.SaveChanges() , with no parametres , I can’t add any records because I get an error : The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object's key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges. Please help me. Thank you.
modified on Monday, August 29, 2011 9:41 PM