LINQ to SQL Sequence contains several elements

I get an error when trying to delete a record from the database.

Here is my current delete code:

Dim BAToRemove = From i In uDC.BookAuthors _
                       Where i.authorID = intAuthorID _
                       And i.bookID = intBookID _
                       And i.main = "N" _
                       Select i Take (1) Distinct

        If BAToRemove.Any Then
            Dim ba As BookAuthor = BAToRemove.First
            uDC.BookAuthors.DeleteOnSubmit(ba)

            Dim cs As ChangeSet = uDC.GetChangeSet

            uDC.SubmitChanges(ConflictMode.ContinueOnConflict)

            If cs.Deletes.Count = 1 Then
                Return True
            Else
                Return False
            End If
        End If

This is the grunt code to try to get it to work. Even without .First and Take (1), the query definitely returns only one result. I used BAToRemove.Count, and also manually checked the table, and there is only one matching row.

Both BookID and AuthorID are primary keys.

I also tried using BAToRemove. First, right in DeleteOnSubmit.

I also tried to copy all the elements of the returned object to a new object and then install and remove, but I get an error that the element already exists (what it does).

, , SingleOrDefault, , .

?

+3
1

DeleteAllOnSubmit(ba) , SQL-, , .

+3

Source: https://habr.com/ru/post/1731779/


All Articles