Getting or attaching an object

I have the following method:

public bool RemoveBookCategories(IDictionary<Books, IList<C_Category>> books)
    {
        _context.Configuration.AutoDetectChangesEnabled = true;

        foreach (var book in books.Keys)
        {
            foreach (var category in books[book])
            {
                if (!_context.ChangeTracker.Entries<Books>().Any(e => e.Entity.BookId == book.BookId))
                    _context.Books.Attach(book);
                if (!_context.ChangeTracker.Entries<C_Category>().Any(e => e.Entity.Id == category.Id))
                    _context.C_Category.Attach(category);

                book.C_Category.Remove(category);
            }
        }

        if (_context.SaveChanges() > 0)
            return true;

        return false;
    }

It works as expected. Sometimes. Other times, I get this error message:

{ " " DataAccess.Plusbog.C_Category " , . " " " "" ", . , . " "" ", " " , " "" ", ." }

, , changetracker. , , , : -/

: -)

+4
2

, , . , , .

0

, : Books, , , , . EF , , . , .

, , , .

0

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


All Articles