Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Foreign Key Constraint Code First EF

If I wanted to disable foreign key constraint on a relationship. I would do the following if I was editing in database:

enter image description here

But I want the disabling of a foreign key constraint on a relationship in my migration file or in my configuration or in my model in the form of annotation in respect to 'Code First' approach. So far I came up with nothing but I tried with .WillCascadeOnDelete(false) though. I also noticed that EF does not care if I take the liberty of disabling foreign key constraint from database but that would violate code first approach in my perspective. So, any suggestion is highly appreciated.

like image 259
lbrahim Avatar asked Feb 26 '14 05:02

lbrahim


1 Answers

One solution I found was to run my own custom migration like :

Sql("ALTER TABLE dbo.ProductItems NOCHECK CONSTRAINT [FK_dbo.ProductItems_dbo.Services_ProductItemTypeId]");

Do not know whether that is the only way. Still open to suggestions.

like image 90
lbrahim Avatar answered Nov 20 '22 06:11

lbrahim