简体   繁体   中英

How can I change foreign key option on MySQL EF 6?

I created a table with wrong foreign key option in MySQL EF 6 so I want to change foreign key option of that table to CASCADE DELETE, but how to?

I made this database on code-first. Is there any solution without re-creating database or manually call SQL?

Just change to mapping as it is needed to be

then create migration script as you did it earlier.

add-migration AlterMyTableFK

after that Update Database as you did it earlier.

update-database

Just make changes you want, then in the "Package Manager Console", execute this line:

add-migration v1 

note that v1 in the above line is for migration versioning and you may name the migration anything you want.

To add a new migration that reflects your changes. then execute this line:

update-database

There is a solution. You can customize the migration code using one of this commands

in Visual Studio

Add-Migration Changes

using PowerShell window

dotnet ef migrations add Changes

Then you can update the database again.

Here's more information

You Can Make changes You Want, Then Open "Package Manager Console"

PM> Enable-Migrations

then :

PM> Add-Migration v1

then:

PM> Update-Database

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