I get this error whenever I try to update one of my models. The update is pretty simple:
Todo bn = service.GetTodos().Single(t => t.todoId == 1);
bn.Note.noteTitle = "Something new";
service.SaveTodo(bn);
And the models have this structure:
- A todo has a note
- Todo has a list of tasks
I have a SaveTodo service that looks something like this:
public void SaveTodo ( TodoWrapper note )
{
using (Repository repo = new Repository(new HpstrDataContext()))
{
if (note != null)
{
Todo todo = repo.Todos.SingleOrDefault(t => 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);
}
}
}
And the Repository SaveTodo method is pretty simple and looks like this:
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();
}
The error occurs in this line in the repository: dc.NoteTable.Attach(todo.Note);. I tried many different things to make this work, but nothing works.
Any help would be greatly appreciated.
source
share