I have a property object and I wanted to delete that from the latest repository collection. Or I can say detach it before saving to the database. There is a Property table which is main and history and detail tables are related to Property via a shared PropertyId as key. When I am detaching the object from the repository and trying to save it it results in an error.
Doing this.
Repository.Detach(P);
Errors out on
Repository.SaveChanges();
Error:
The INSERT statement conflicted with the FOREIGN KEY constraint “FK_History_Property”.
The conflict occurred in database “database”, table “dbo.Property”, column ‘PropertyId’.
The statement has been terminated.
Try to delete records from other tables that references to the primary key of the record you are trying to delete before deleting the record.
Ex:
Transaction Table
Transaction Detail
TransactionIDhere is a foreign key (Primary Key From other table). Before you could delete the parent record on transaction table you have to delete first all records that references to its PrimaryKey to avoid FOREIGN KEY CONSTRAINT ERRORSEx:
If you want to delete transaction 1 from the database, you have to delete transaction details(Transaction Detail 1 and 2) first before you will be allowed to delete transactionID 1