Problem when inserting a new Row
-
Hi, I am developing an application in which we use LINQ to interact with the Database SQLCE. We have a contact table of which ContactId is the primary key.We use compiled query to get the next Maximum value for the primary key. GetAllContactId(objCON).Max() + 1 and LINQ for the compiled query is as follows, public static readonly Func> GetAllContactId = CompiledQuery.Compile> ( (con) > from contact in con.Contact select contact.ContactId ); We are frequently deleting and updating records from this Contact table. The problem comes when deleting the last record from the table and try to insert a new record using with the same primary key an exception displays "Cannot add an entity with a key that is already in use." and the stack trace is as follows at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited) at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects() at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges() . I am not able to figure out the actual problem . Since my table doesnot contain a record with the currenlty generated Primary key value but the link says it exists. thanks Nitheesh
Jose Jo Martin http://www.simpletools.co.in
-
Hi, I am developing an application in which we use LINQ to interact with the Database SQLCE. We have a contact table of which ContactId is the primary key.We use compiled query to get the next Maximum value for the primary key. GetAllContactId(objCON).Max() + 1 and LINQ for the compiled query is as follows, public static readonly Func> GetAllContactId = CompiledQuery.Compile> ( (con) > from contact in con.Contact select contact.ContactId ); We are frequently deleting and updating records from this Contact table. The problem comes when deleting the last record from the table and try to insert a new record using with the same primary key an exception displays "Cannot add an entity with a key that is already in use." and the stack trace is as follows at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects(MetaType type, Object item, Dictionary`2 visited) at System.Data.Linq.ChangeProcessor.TrackUntrackedObjects() at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) at System.Data.Linq.DataContext.SubmitChanges() . I am not able to figure out the actual problem . Since my table doesnot contain a record with the currenlty generated Primary key value but the link says it exists. thanks Nitheesh
Jose Jo Martin http://www.simpletools.co.in
Before adding the new record have you commited all previous updates (i.e. the deletion) to the database? If you've only marked it as deleted Linq is very likely to get confused as it will still exist within its internal collections.