2

There is typical many to many relationship between books and authors. I've got an error when I tried to update my authors. The full name of error is "SqlException: Violation of PRIMARY KEY constraint 'PK_BookAuthor'. Cannot insert duplicate key in object 'dbo.BookAuthor'. The duplicate key value is (2, 2)."

If I don't use UpdateAssociatedObject function, all is okay. But if I use it, I get the error when I try to save the changes.

 var parseBook = Mapper.Map<BookViewModel, Book>(viewModel);
        Book viewModelBook = parseBook;
        var book = db.Books.FindOne(viewModel.BookId);
        book.BookId = viewModelBook.BookId;
        book.BookName = viewModelBook.BookName;
        book.Genre = viewModelBook.Genre;
        book.Pages = viewModelBook.Pages;
        book.Publisher = viewModelBook.Publisher;
        db.SaveChanges();
        UpdateAssociatedObject(book, viewModelBook);
    }
    public void UpdateAssociatedObject(Book bookForUpdate, Book book)
    {
        bookForUpdate.BookAuthors = book.BookAuthors;
    //error is here
        db.SaveChanges();
    }

The value of viewModelBook after mapping: The value of BookAuthor property Models(some properties removed for brevity)

 public class Book
{
    public int BookId { get; set; }

    public List<BookAuthor> BookAuthors { get; set; }
}
public class Author
    {
        public int AuthorId { get; set; }
        public string AuthorName { get; set; }
        public List<BookAuthor> BookAuthors { get; set; }
    }
 public class BookAuthor
{
    public int BookId { get; set; }
    public Book Book { get; set; }

    public int AuthorId { get; set; }
    public Author Author { get; set; }
}
public class BookViewModel
{
    public int BookId { get; set; }

    public virtual List<AuthorViewModel> Authors { get; set; }    
}
public class AuthorViewModel
    {
        public int AuthorId { get; set; }

        public string AuthorName { get; set; }
    }
public class Library : DbContext
{
    public DbSet<Book> Books { get; set; }

    public DbSet<Author> Authors { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<BookAuthor>().HasKey(sc => new { sc.BookId, sc.AuthorId });
    }
}

Mapper:

 CreateMap<BookViewModel, Book>()
            .ForMember(b => b.BookId, opt => opt.MapFrom(b => b.BookId))
        .PreserveReferences()
        .ForMember(b => b.BookAuthors, opt => opt.MapFrom(b => b.Authors
        .Select(a => new BookAuthor { AuthorId = a.AuthorId })));

UPD: BookId jumps after creating a book(in steps of 1000).

After adding Include() to Find method, the error changed on

System.InvalidOperationException: 'The instance of entity type 'BookAuthor' cannot be tracked because another instance with the same key value for {'BookId', 'AuthorId'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.'

10
  • I've had a similar problem, but in relationships 1 to n. At the beginning of the UpdateAssociatedObject function, is the bookForUpdate.BookAuthors object null? If it is not null, you can try to use the methods of the collection (eg Add) Apr 16, 2018 at 14:22
  • @Gustavo Rossi Muller sorry for delay, unfortunately bookForUpdate.BookAuthors object is null.
    – Vlad
    Apr 16, 2018 at 15:27
  • Even if it's not null, the error is the same(
    – Vlad
    Apr 17, 2018 at 9:10
  • 1
    I do not know much about AutoMapper, but I think the following is happening: You have a list of authors, some new, others removed, and others updated. I think the updated authors are generating the problem. The EF uses an object as a proxy to refer to the records in the base, if it is a normal object it will try to give insert, even if the PK already exists. Can you check the type of object that is on your list (using watch)? Maybe it's an EF class that inherits from Author, in that case I'm talking nonsense. Apr 17, 2018 at 12:33
  • 1
    try include a DBSet in the context, and use they to retrieve. Apr 17, 2018 at 13:33

2 Answers 2

1

Finally it works! Below you can see the differences from original code.

P.S Perhaps you know better algorithm for Remove method, if so you can type it in comments then I will change the answer.

BookRepository:

 public Book Find(int id)
    {
        Book searchedBook = db.Books
          .Include(b => b.BookAuthors)
          .ThenInclude(b => b.Author)
          .SingleOrDefault(b => b.BookId == id);

        return searchedBook;
    }

BookService:

public void UpdateObject(BookViewModel viewModel)
        {
            Book parsedBook = Mapper.Map<BookViewModel, Book>(viewModel);
            //Book viewModelBook = parseBook;
            var book = db.Books.Find(viewModel.BookId);
            book.BookId = parsedBook.BookId;
            book.BookName = parsedBook.BookName;
            book.Genre = parsedBook.Genre;
            book.Pages = parsedBook.Pages;
            book.Publisher = parsedBook.Publisher;
            UpdateAssociatedObject(book, parsedBook);
        }
public void UpdateAssociatedObject(Book bookToUpdate, Book viewModelBook)
        {
            AddUpdatedAuthors(bookToUpdate, viewModelBook);
            RemoveUpdatedAuthors(bookToUpdate, viewModelBook);
            db.SaveChanges();
        }
     private void AddUpdatedAuthors(Book bookToUpdate, Book viewModelBook)
        {
            foreach (var authors in viewModelBook.BookAuthors)
            {
                var searchBookAuthor = bookToUpdate.BookAuthors.Find(b => b.AuthorId == authors.AuthorId);
                if (searchBookAuthor == null)
                {
                    bookToUpdate.BookAuthors.Add(authors);
                }
            }
        }
        private void RemoveUpdatedAuthors(Book bookToUpdate, Book viewModelBook)
        {
            int countOfAuthors = bookToUpdate.BookAuthors.Count;
            for (int i = 0; i < countOfAuthors; i++)
            {
                BookAuthor searchBookAuthor = null;
                foreach (var viewModelAuthors in viewModelBook.BookAuthors)
                {
                    if (viewModelAuthors.AuthorId == bookToUpdate.BookAuthors[i].AuthorId)
                    {
                        searchBookAuthor = viewModelAuthors;
                    }
                }
                if (searchBookAuthor == null)
                {
                    bookToUpdate.BookAuthors.Remove(bookToUpdate.BookAuthors[i]);
                    i--;
                    countOfAuthors--;
                }
            }
        }
0

The problem looks with db.SaveChanges(). You are calling db.SaveChanges with same context multiple times in

db.SaveChanges();

UpdateAssociatedObject(book, viewModelBook);

and in UpdateAssociatedObject(book, viewModelBook); Try removing one db.SaveChanges().

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.