Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot attach an entity that already exists
    primarykey
    data
    text
    <p>I get this error whenever i try to update one of my models. The update is pretty simple:</p> <pre><code>Todo bn = service.GetTodos().Single(t =&gt; t.todoId == 1); bn.Note.noteTitle = "Something new"; service.SaveTodo(bn); </code></pre> <p>And the models have this kind of structure:</p> <ul> <li>A Todo has a Note</li> <li>A Todo has a list of Tasks</li> </ul> <p>I have my service's SaveTodo look a little something like this:</p> <pre><code>public void SaveTodo ( TodoWrapper note ) { using (Repository repo = new Repository(new HpstrDataContext())) { if (note != null) { Todo todo = repo.Todos.SingleOrDefault(t =&gt; t.todoId == note.todoId); if (todo == null) { todo = new Todo(); todo.Note = new Note(); } todo.dueDate = note.dueDate; todo.priority = (short)note.priority; todo.Note.isTrashed = note.Note.isTrashed; todo.Note.permission = (short)note.Note.permission; todo.Note.noteTitle = note.Note.noteTitle; repo.SaveTodo(todo); } } } </code></pre> <p>And the Repository's SaveTodo method is pretty simple and looks like this:</p> <pre><code>public void SaveTodo ( Todo todo ) { if (todo.Note.noteId == 0) { dc.NoteTable.InsertOnSubmit(todo.Note); } else { dc.NoteTable.Attach(todo.Note); dc.NoteTable.Context.Refresh(RefreshMode.KeepCurrentValues , todo.Note); } if (todo.todoId == 0) { dc.TodoTable.InsertOnSubmit(todo); } else { dc.TodoTable.Attach(todo); dc.TodoTable.Context.Refresh(RefreshMode.KeepCurrentValues , todo); } dc.SubmitChanges(); } </code></pre> <p>The error is being thrown at this line in the Repository: <code>dc.NoteTable.Attach(todo.Note);</code>. I've tried a lot of different things to get this to work but nothing seems to work. </p> <p>Any help would be greatly appreciated</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload