Search code examples
c#asp.net-coreentity-framework-core

An error occurred while saving the entity changes. See the inner exception for details


I'm new to this and I'm here to seek some help from the experts🥺🥺

This project is using ASP.NET Core & C#.

I am trying to initialize my database.. However, I keep facing these issues and I am not sure how to debug it. [1]: https://i.sstatic.net/RtT6V.png

This project required 3 databases. An Expense Database, a Category Database and a Sub-Category Database. Below shows how are they going to connect with each other. [2]: https://i.sstatic.net/FoU8F.png [3]: https://i.sstatic.net/KHxqf.png [4]: https://i.sstatic.net/1ZTnb.png

Thanks in advance for your response🌈


Solution

  • Because you don't provide expenses, So I don't know what's in that, I just can give you some advice about this error message .The error occurs in your Category table, The way a FK works is it cannot have a value in that column that is not also in the primary key column of the referenced table. It seems that You are trying to insert a record with a value in the foreign key column that doesn't exist in the foreign table.

    For example: If you have Books and Authors tables where Books has a foreign key constraint on the Authors table and you try to insert a book record for which there is no author record.

    So i suggest you check if any of the situations I mentioned above are found in your code.