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:
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.'