Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi everybody
whats the cause and solution of the following error?

InvalidOperationException: The value of 'Author_Book.BookID' is unknown when attempting to save changes. This is because the property is also part of a foreign key for which the principal entity in the relationship is not known.

ASP.Net Core 5
EF Core 5.0.4

https://s17.picofile.com/file/8429178926/2021_03_30_162918.png[^]

Source : Dropbox - BookShop.rar - Simplify your life[^]

What I have tried:

in ASP.Net Core 2, Entity Framework Core 2 no problem...
Posted
Updated 29-Nov-22 20:18pm
v2
Comments
[no name] 30-Mar-21 11:46am    
Find out what "BookID" is pointing to. Sounds like that "book" / id doesn't exist.
ahmadamiri 30-Mar-21 12:26pm    
please see source "https://www.dropbox.com/s/mr7d16kq4f6g8nr/BookShop.rar?dl=0"
then run "Admin/Books/create"

1 solution

Hi!

You forgot to write
C#
await _context.Books.SaveChangesAsync();

after this:
C#
await _context.Books.AddAsync(book);
 
Share this answer
 
v2
Comments
Richard Deeming 30-Nov-22 4:37am    
The error message in the question clearly states that the error occurred when attempting to save changes. That would not happen if the OP had forgotten to call SaveChanges or SaveChangesAsync.
Zohidbek Mengliboyev 30-Nov-22 6:22am    
Maybe, but I had a similar situation. That's why I gave the solution that worked for me.
Richard Deeming 30-Nov-22 6:59am    
So you resurrected a question from last year to post an answer to a completely different question?

That sort of behaviour will get you banned from the site. If you're going to answer an old question, you need to make sure you're answering the question that was asked.
Zohidbek Mengliboyev 30-Nov-22 7:40am    
You misunderstood my previous reply. My situation was the same as that person asked. When I couldn't find the answer, I googled it. Then I found recently this guy's project on CodeProject. Interestingly, his project's errors and exceptions were very similar to mine. Then I wrote an answer to this person's question only after I found the answer for my project. And this is not kind of behaviour that I will banned.
Richard Deeming 30-Nov-22 8:01am    
The errors may be similar, but the fact that the error explicitly states "when attempting to save changes" suggests that "you forget to call SaveChangesAsync" is not the cause of the OP's error.

And to be honest, I fail to see how you could get an error "when attempting to save changes" and actually believe that the solution was that you had forgotten to save the changes.

Granted, the question you're answering is extremely poor; expecting someone else to download your entire project to diagnose the problem is a great way to get your question ignored or deleted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900