.net - LinqtoSQL - clearing then inserting -


Although this is probably not the best practice, I am trying to clear a series of records from a database table , And then insert a series of records - some of which can be in the original series, and others who can be new I'm using linqtosql in C #. The pseudo code I have to do is down this; It fails with "a unit which is already in use, can not connect an entity with".

  (dbDataContext reference = new dbDataContext () {// I also tried to use table.LinkedTable. Clear (); But this shows the same error, while (table Linked.Count ()> 0) {table.LinkedTable.RemoveAt (0);} foreach (SelectedItems.Items in SelectedItem Item) {LinkedTable lt = new LinkedTable); Lt.id = table.id; Lt.SomeValue = item.SelectedValue; Table.LinedTable.Add (lt);} context.SubmitChanges ();}  

it looks like linqtosql

edit if I add one, so I do not even believe that I have tried to remove the objects before adding the objects, causing a duplication error. Submit after deletion, then I end up with this message.

"An attempt was made to remove the relation between a table and a linked table, however, the foreign key of a relation (linkdable.id) can not be

You can try to use this method:

  (dbDataContext context = new dbDataContext () {/ / Table.LinkedTable} is an EntitySet of LinkedTable objects, // We remove from the "linkedtable" table, the records are related to "//" table: context.LinkedTable.DeleteAllOnSubmit (table.Link EdTable); Foreign exchange (List item items in selected items) (Linked LT = New Linked Table); Lt.id = table.id; Lt.SomeValue = item.SelectedValue; table.LinedTable.Add (lt);} Context. SubmitChanges ();}  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -