Some help with foreign key in .net
When im trying to add a foreign key to a table it shows an error "ALTER table "
14 Replies
I'm afraid we need significantly more information to help you.
* What is the structure of the table you're trying to add a foreign key to?
* What is the foreign key pointing to?
* What is the exact, full error message you've got?
i have 2 tables : Puna(it means Jobs) and Categories . In Category i have attributes like :
i added this table to mssql correctly
so i did this migration through console and it was added successfully
then i have other class Puna(Jobs) with those attributes:
why aren't you using migrations?
oh, you did
im using them
i misread
i added this table to mssql correctly
to mean you weren'tthen when i try to add these foreign key when i add migration then update-database it shows this : "The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Punet_Categories_CategoryId". The conflict occurred in database "Jobify", table "dbo.Categories", column 'Id'."
i dont have this attribute still in my database
isn't the ForeignKey attribute supposed to go on the key property, not the navigation property?
technically you don't need it at all as it should be figured out by convention
idk im facing this error about 2 days
watched some videos still didnt get the result i want
the official documentation is the best place to look
Mapping attributes (aka Data Annotations) for relationships - EF Core
Using mapping attributes (also know as Data Annotations) to configure Entity Framework Core relationships
i guess that attribute works both ways
so instead using ForeignKey annotation maybe use this [ForeignKey(nameof(Blog))]
still it doesnt work
how can i alter the table in console
if you're using migrations, you don't
altering the database schema outside of migrations is how you end up with problems like this
so i should store them in another database ?
i started in a new database and occured the same problem <<<<The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Punet_Categories_CategoryId". The conflict occurred in database "Jobify", table "dbo.Categories", column 'Id'>>>>