简体   繁体   中英

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:

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM